8

I have this piece of code

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  msg: String;
begin
  msg := 'Do you really want to exit?';

  if MessageDlg(msg, TMsgDlgType.mtConfirmation,
    [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrNo then
    CanClose := False
  else
    CanClose := True; { FIXME: don't want to work on Android }
end;

It works perfectly on Windows. Application closes if I choose 'Yes'. However, application does NOT close on Android. What I am doing wrong?

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Edijs Kolesnikovičs
  • 1,627
  • 3
  • 18
  • 34

4 Answers4

13

Having the application close when the last form is closed is a Windows thing. An Android app will keep running.

To close the app on Android, call SharedActivity.finish from the FMX.Helpers.Android unit.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
7
uses 
  FMX.Platform.Android;

procedure TForm2.SpeedButton1Click(Sender: TObject); 
begin 
  MainActivity.finish; 
end; 
kobik
  • 21,001
  • 4
  • 61
  • 121
Helios
  • 71
  • 1
1

I tried all combinations.

 - SharedActivity.Finish - NOT WORKING FOR ME
 - MainActivity.Finish - NOT WORKING FOR ME
 - Application.MainForm.DisposeOf - NOT WORKING FOR ME

This is works for me :

 FreeAndNil(Application);
xJernej
  • 194
  • 2
  • 3
  • 14
0

Calling Halt also closes the application.

Oussama Al Rifai
  • 323
  • 1
  • 6
  • 16
  • 1
    With Halt, the execution path immediately stops. Your finally in try...finally statements don't get called. Your form's OnClose won't be called, etc. – Marcus Adams Oct 10 '13 at 13:04
  • Thanks Marcus. By the way, I tried SharedActivity.finish in an application where I use TGuestureManager, it didn't close the application. it is still in the running application list. moreover when I try to activate it it hangs. Am I missing something? When I run it in debugger mode. I get exception in TGuestureManger.Notification proceedure in 1st line. – Oussama Al Rifai Oct 12 '13 at 08:04
  • Do you have any other activities running? – Marcus Adams Oct 12 '13 at 14:31
  • I new to android.. I call another window and then I close it... does that mean that it is still open? and if yes, how should I close it? – Oussama Al Rifai Oct 12 '13 at 17:18