Question

In Delphi for windows there is no problem to Free (Form.free) closed secondary dynamicaly-created form because where is "ShowModal" method. But Delphi for Android does not support Form.ShowModal, and we have to use Show method. But I figured out what when I close (Form.close) secondary form it is still in memory and even run code Onresize event (???). What is the best way to Free forms in non-Modal call?

In another words: How do I close a form from an OnClick event handler on that form, and ensure that the form's destructor runs?

Was it helpful?

Solution

Update

See important note below.


In XE5 for Android there is a possibility to show a form with modal results, an overloaded ShowModal procedure using an anonymous method:

procedure ShowModal(const ResultProc: TProc); overload;

You can find it described in this article by Marco Cantu, Delphi XE5 Anonymous ShowModal and Android.

Here is the example how to use this procedure:

var
  dlg: TForm1;
begin
  dlg := TForm1.Create(nil);
  // select current value, if avaialble in the list
  dlg.ListBox1.ItemIndex := dlg.ListBox1.Items.IndexOf(Edit1.Text);
  dlg.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      if ModalResult = mrOK then
      // if OK was pressed and an item is selected, pick it
        if dlg.ListBox1.ItemIndex >= 0 then
          edit1.Text := dlg.ListBox1.Items [dlg.ListBox1.ItemIndex];
      dlg.DisposeOf;  // Wrong !!!, see note below
    end);

Note that the dlg.DisposeOf; will force the form to be destroyed, overriding the ARC automatic handling.

You can also find a description in the documentation, Using Modal Dialog Boxes in Mobile Apps and here, ShowModal Dialogs in FireMonkey Mobile Apps.


As found by others, http://www.malcolmgroves.com/blog/?p=1585, calling DisposeOf inside the anonymous method is wrong because the anonymous frame must be able to handle ModalResult from a valid object. Use this pattern instead to free the modal dialog, Freeing Your Modal Dialog Box.

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

OTHER TIPS

Don't forget to set in the ObjectInspector

ModalResult = mrOK

or in your

procedure TForm1.ExitButtonClick(Sender: TObject);
begin
   ModalResult := mrOK;
end;

for the example dlg.ShowModal above!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top