How to detect the termination of a program in Android?

六眼飞鱼酱① 提交于 2019-12-04 13:18:03

In iOS applications seldom close but enter a background mode.
This is why the OnClose event does not fire. I suspect that killing an app by clicking on the 'x' in the taskmanager actually forcefully terminates the app, but haven't tested this. in any case this use case is too rare to code against.
In Android things work pretty much the same.

Luckily Anders Ohlsson has written a very informative blog post about this subject, see here: http://blogs.embarcadero.com/ao/2013/05/01/39450.
The following post builds on that to catch the actual backgrounding https://forums.embarcadero.com/message.jspa?messageID=558241

The trick is to register for application events. See: http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Platform.TApplicationEvent

Some sample code for iOS don't have Android handy, sorry.
Copy from the above forum:

unit Unit1;

interface

uses
  System.SysUtils, System.Classes, FMX.Forms, FMX.Platform;

type
TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
private
public
  function AppEvent(AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;
end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

function TForm1.AppEvent(AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;
begin
  if AAppEvent = TApplicationEvent.aeEnteredBackground then begin
    // do something here for when the app is sent to background
  end;
  Result := True; // let iOS know it worked...
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  AppEventSvc: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(AppEventSvc)) then
    AppEventSvc.SetApplicationEventHandler(AppEvent);
end;

end. 

Obviously these events should have triggered sensible event handlers in FMX.Platform.TApplication but they don't. http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Forms.TApplication_Events
Perhaps you should extend TApplication to add these eventhandlers so that sanity can be preserved.
I recommend filing a QA report.

Here's a suggestion for the extended TApplication class.

type
  TnotifyApplication = class(FMX.Platfrom.TApplication) 
  private
    FOnStop: TnotifyEvent;
  protected
    procedure AppEvent(AAppEvent: TApplicationEvent; AContext: TObject): boolean;
    procedure SetOnStop(value: TNotifyEvent);
    procedure DoOnStop;
  public
    property OnStop: TNotifyEvent read FOnStop write SetOnStop;
  end;

implementation

procedure TNotifyApplication.SetOnStop(value: TNotifyEvent);
begin
  if Assigned(value) then begin  
    //register for the notification to call AppEvent
  end else begin
    //
  end;
end;

procedure TNotifyApplication.DoOnStop;
begin
  if Assigned(FOnStop) then FOnStop(self);
end;

procedure TNotifyApplication.AppEvent(AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;  
begin
  //call the relevant do... Call depending in the exact event. 
Sagar Vaishnav

When the user leaves your activity(Screen), the system calls onStop() to stop the activity. If the user returns while the activity is stopped, the system calls onRestart(), quickly followed by onStart() and onResume(). Hence writing the code and placing it in the body of onStop() method is recommended.

http://developer.android.com/images/training/basics/basic-lifecycle-stopped.png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!