Good day. I am writing in C ++ Builder in Embarcadero Xe8. I do mobile application project on Ios and android and faced such problem: I can not catch the phone lock screen event. I used to always do so:
bool TForm1::HandleApp(TApplicationEvent a, TObject *x)
{
if (a == TApplicationEvent::EnteredBackground)
{
MediaPlayer1->Stop();
}
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
_di_IFMXApplicationEventService a;
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXApplicationEventService), &a))
{
a->SetApplicationEventHandler(TForm1::HandleApp);
}
}
But an error:
\Unit1.cpp(33): cannot initialize a parameter of type 'TApplicationEventHandler' (aka 'bool (closure *)(Fmx::Platform::TApplicationEvent, System::TObject __borland_class *__strong) __attribute((pcs("aapcs-vfp")))') with an lvalue of type 'bool (__closure *)(Fmx::Platform::TApplicationEvent, System::TObject __borland_class *__strong)' FMX.Platform.hpp(252): passing argument to parameter 'AEventHandler' here
I Do not know what else to try to do! Could you please help me?
Your
HandleApp()method is missing the__fastcallcalling convention:Also, your call to
SetApplicationEventHandler()needs to be like this instead:This is important because the event handler is a
__closure, so it carries two pointers inside of it - a pointer to the class method to call, and a pointer to the object instance that the method is called on (thethisvalue of the method). When you pass the handler by class name only, the compiler doesn't know which object instance to act on, and thus cannot populate the__closure. The syntax above allows the compiler to see thatHandleAppshould be associated with theForm1object.