1
votes

I have a Firemonkey Multi Device(Android & iOS) Project in Rad Studio 10 Seattle. I want to call a form with showmodal from a method in a unit and give the modalresult back with the function.

I've tried the following examples below:

function ShowMyForm: TModalResult;
var
  form: TForm1;
begin
  form:= TForm1.Create(nil);
  form.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      result := ModalResult;
    end);
end;

function ShowMyForm: TModalResult;
var
  form: TForm1;
begin
  form:= TForm1.Create(nil);
  result := form.ShowModal;
end;

With the inline procedure the function can't access the result.

And just calling TForm.ShowModal doesn't work on a multi device project.

Is there an other way to achieve this?

1
From documentation: Caution: Modal dialog boxes are not supported in Android apps. Instead of calling ShowModal, you should call Show, and have the form return and call your event. We recommend that you not use modal dialogs on either of the mobile platforms (iOS and Android) because unexpected behavior can result. Not using modal dialogs eliminates potential problems in debugging and supporting your mobile apps. - LU RD
In mobile compilers, ShowModal is not blocking. This means that your function ShowMyForm is complete before the modal form is executed. Instead do as docs says, call Show and let the form return and call your event. - LU RD
@LURD only problem there is that I want to use it for a lot of different things. So not 1 single event. - Remi
Pass the event code in an anonymous method. That way it is not tied to a single solution. - LU RD
@RemyLebeau updated his answer delphi XE-5 cross platform development - calling a showmodal form. I suggest you try it and report back if it is ok. - LU RD

1 Answers

3
votes

I've solved my problem by adding an inline procedure that's called when modalresult equals mrOk.

Code below:

Method for showing my Form with showmodal

procedure ShowMyForm(event: TProc = nil);
var
  form: TForm1;
begin
  form:= TForm1.Create(nil);
  form.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      if (ModalResult = mrOk) and Assigned(event) then
        event;  
    end);
end;

The call of the procedure with an inline procedure.

ShowMyForm(
      procedure
      begin
          // Code that you want to do on mrOk
      end);