3
votes

I have the following Delphi code for showing a modal message on Android which worked fine on 10.1 Berlin, but stopped working on Delphi 10.2.1 Tokyo. This procedure now hangs the Android app.

procedure customShowMessage(AMessage: string);
//good idea to have our own procedure that we can tweak, as even for VCL and windows, we have done show message differently over the years due to all sorts of funny problems
var
  LModalWindowOpen: boolean;
begin
  LModalWindowOpen := true;

  TDialogService.MessageDialog(AMessage, TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, 0,
         procedure(const AResult: TModalResult)
         begin
           LModalWindowOpen := false;
         end);

  while LModalWindowOpen do
    begin
      Application.ProcessMessages; //since 10.2 Tokyo, popup never shows and this loops forever
    end;
end;

I suspect it possibly has something to do with the change in Tokyo as to how the app runs in the main thread. Not sure what I can replace Application.ProcessMessages with that will let the dialog show, so that the user can click on something.

I have a lot of places this is used, so changing it to work using a callback is going to be a lot of work, and restructuring.

3
Application.ProcessMessages() is broken on Android in Tokyo (along with a lot of other things - do you have the latest Update installed?). Besides, modal dialogs are not supported on Android anyway. Embarcadero's documentation says as much. FMX has supported async dialogs since XE5 to address this. You need to rewrite this code to work asynchronously. Get rid of the ProcessMessages loop and let the dialog notify you when it has been closed. Do not block the calling thread waiting for the dialog to close. It is the proper way to handle this. You should not have ignored this in your design.Remy Lebeau
Thanks Remy, I double checked for updates. The hotfixes currently available are for Tokyo 10.2. I have used the latest installer which is Tokyo 10.2 update. Looks like the long term solution will be to change to using callbacks.Maya

3 Answers

3
votes

On Android we have only asynchronous dialog boxes. If we want they act as modal dialog box, we have to do ourself.

The solution with a ProcessMessage loop is an idea, but I don't think it's the best approach.

An other one is to add a transparent (or opaque) layout (or rectangle) on your form before displaying the dialog box and when you have an answer, you can remove the blockin layout.

You can also use TFrameStand from Andrea Magni (downloadable directly from GetIt) who propose to use a TFrame as a dialog box. https://github.com/andrea-magni/TFrameStand

0
votes

I have restructured a lot of code to use asynchronous callbacks, but where callback hell ensues (especially on large existing projects), I have found the following works:

Instead of just using Application.ProcessMessages, I am now calling CheckSynchronize as well.

procedure TfrmStock.WaitForModalWindowToClose;
begin
  while FModalWindowOpen do
    begin
       Sleep(40);
       Application.ProcessMessages;
       CheckSynchronize;
    end;
end;
-1
votes

I think the following code should work:

function ShowMessageOKCancel(AMessage: String): String;
var
  lResultStr: String;
begin
  lResultStr:='';
  TDialogService.PreferredMode:=TDialogService.TPreferredMode.Platform;
  TDialogService.MessageDialog(AMessage, TMsgDlgType.mtConfirmation,
    FMX.Dialogs.mbOKCancel, TMsgDlgBtn.mbOK, 0,
    procedure(const AResult: TModalResult)
    begin
      case AResult of
        mrOK: lResultStr:='O';
        mrCancel:  lResultStr:='C';
      end;
    end);

  Result:=lResultStr;
end;

When you call this function, it should show a dialog with your message and the two buttons OK and Cancel. The return value will indicate which button was clicked.