4

I am having the dandiest time trying to figure out why my modal form will not close! Using Delphi XE-5 and FireMonkey Mobile App (Android), i followed the the info "ShowModal Dialogs in FireMonkey Mobile Apps"

for demo purposes, i created a new Firemonkey Mobile delphi application and added a secondary firemonkey mobile form. From the main form, i use the code from the article:

procedure TForm1.Button1Click(Sender: TObject);
var
  Form2: TForm2;
begin
  Form2 := TForm2.Create(nil);

  Form2.ShowModal(procedure(ModalResult: TModalResult)
    begin
      if ModalResult = mrOK then
      begin
        //
      end;
      Form2.DisposeOf;
    end);

end;

On the secondary form, i assign the "Ok" and "Cancel" buttons modalresult property to "mrCancel" and "mrOK", respectively. However, when the modal dialog is shown, neither button makes the dialog close. I even tried adding onClick events and assigning the modalresult by code. Why wont the form close? I guess I need assurance that I did everthing right and possible its my PHONE (device)?

LuvRAD
  • 118
  • 1
  • 4
  • 13
  • The issue doesn't seem to be in the code posted. Look elsewhere. – Marcus Adams Mar 06 '14 at 18:07
  • @KenWhite I have read that article and as you can see, my code is from it. Is there something I am missing in the article related to the actual closing of the form using the OK or Cancel button? – LuvRAD Mar 06 '14 at 22:09
  • @MarcusAdams what would you suggest. I sent the compiled APK file to my son and he tested it on a Nexus device and it did the same exact thing, so it is within the code or runtime code. – LuvRAD Mar 06 '14 at 22:10
  • @Marcus is correct. If your modal dialog won't close, it's not in the code you posted here. (The article I referred you to before discusses how modal windows are different on Android - I believe that "separate activity" is the phrase Marco used.) `ShowModal` really isn't modal in the Windows sense. – Ken White Mar 06 '14 at 22:32
  • @KenWhite Ive read the article twice, and i have asked you "what part of the article refers to the modal dialog side (i.e. the clicking of the buttons to close the dialog?)? Do you just merely set the buttons modalresult property to "mrOK" and "mrCancel" or is there something else that you do. Using the code above and setting the buttons modalresult property does not close the modal dialog. Please explain. – LuvRAD Mar 07 '14 at 13:26
  • @KenWhite As I stated above, its not in my device either. I have tested it on a Samsung Galaxy Note III and I sent the APK file to my son who tested it on a Nexus tablet. This is a very simple test demo with no other code. I created a main form (form1) with one button on it that uses the code above. I then created the secondary form (Form2) with two buttons (OK & Cancel) each with their modalresult property set to "mrOK' and "mrCancel" respectively. THAT IS IT! No other code. And its been tested on two separate Android devices. – LuvRAD Mar 07 '14 at 13:27
  • @KenWhite, see my answer. The observation by the OP is correct. The examples and documentation was wrong. – LU RD Nov 11 '14 at 22:39
  • I have used this example to show a Dialog Box in my Android app in Firemonkey but I get next error: "android.view.ViewRootImpl$CalledFromWrong ThreadException: Only the original thread that created a view hierarchy can touch its views." How I can solve it? – KryNaC May 10 '16 at 11:55

1 Answers1

5

In order to close your modal dialog, use this pattern:

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := TCloseAction.caFree;
end;

and remove your call Form2.DisposeOf;, since the ModalResult setter needs to operate on a valid object.

The documentation has been updated in XE7, see Using FireMonkey Modal Dialog Boxes.

See also ShowModal on Android for the details why DisposeOf is wrong.

LU RD
  • 34,438
  • 5
  • 88
  • 296