1
votes

In CloseQueryEvent, i have added the MessageDlg code for the Application Close confirmation and when i'm running the application in Windows, Application was running properly if i'm passing CanClose =True. But this messageDialogue is not working in Android and please suggest me is there any other way to handle the closeQuery Event. And also I have seen example of How to close android app in Delphi-XE5 Firemonkey application?

3
If you have code that is not working, you must show us that code, otherwise we can't help you. Have you tried the solution (which is answered 3 times) in the question you linked?Jerry Dodge
Even on Windows, a dialog to confirm closing an application goes against Microsoft's recommended practices. If you try to confirm closing an app when the Windows asks you to close it, the OS will forcibly terminate your app. The feature remains more due to legacy, and I suggest rather not prompting. As long as it's easy to reopen your app, why waste time asking to confirm a close request?Disillusioned

3 Answers

2
votes

Modal dialogs do not work on mobile platforms. You have to use the asynchronous versions that take an anonymous procedure as input, and then perform your desired actions inside that procedure when the dialog is closed. For example:

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
  MessageDlg('Do you really want to exit?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
        Application.Terminate;
    end
  );
end;
1
votes
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;  var KeyChar: Char; Shift: TShiftState);
begin
  if Key = vkHardwareBack then
  begin
    MessageDlg('Do You Want To Close This Application', TMsgDlgType.mtConfirmation,
    [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
      begin
        Application.Terminate;
        Exit;
      end
    end);
    Key := 0;
  end;
end;

This code is working fine for me

0
votes

Based on Remy's answer.

Modal dialogs do not work on mobile platforms. You have to use the asynchronous versions that take an anonymous procedure as input, and then perform your desired actions inside that procedure when the dialog is closed. For example:

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
  MessageDlg('Do you really want to exit?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
        Application.Terminate;
    end
  );
end;

In Android, pressing the back button will not trigger that event. To fix that, we have to change the back button behaviour in order to call the function CloseQuery:

procedure TFormMain.FormKeyUp(Sender: TObject; var Key: Word;  var KeyChar: Char; Shift: TShiftState);
begin
    if Key = vkHardwareBack then
    begin
        TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
        if (FService <> nil) and (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
        begin
            // Back button pressed, keyboard visible, so do nothing...
        end else
        begin
            Key := 0;
            CloseQuery;
        end;
    end;
end;