Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

ShowModal on Android

Discussion in 'Delphi Android And IOS' started by N0body, Jul 14, 2014.

  1. N0body
    Online

    N0bodyN0body is a Verified Member DelphiFan Administrator Staff Member DF Staff

    FireMonkey does a lot of work to allow you to write one set of code that compiles and runs on different platforms.

    Not only that, it also does a lot of work to make many of the coding patterns that some of you are used to from the VCL on Windows, work unchanged on other platforms.

    That’s great, but in some cases, re-using old patterns is not desirable. ShowModal is a case in point.

    If you’ve come from VCL, you’ve written code like this a million times:

    Code:
    procedure THeaderFooterForm.btnPickClick(Sender: TObject);
    var
      dlg: TForm1;
    begin
      dlg := TForm1.Create(nil);
      try
        if dlg.ShowModal = mrOK then
          // do something here
          ...
      finally
        dlg.Free;
      end; 
    end;

    The key things to note are that:

     the call to ShowModal is bHIDE-THANKSing. By the time the comparison to mrOK is done, the child form has been shown and closed.
    dlg.Free actually causes the form’s destructor to run and the memory be reclaimed.
    All good. However, two things trip us up when we move this pattern to mobile:

    With ARC, a call to Free does not invoke the destructor. It decrements the object’s reference count. If it is greater than zero, the object won’t be destroyed. If you want to force the destructor to run, call DisposeOf instead.
    Android doesn’t really have the concept of modal forms. As a result, a call to ShowModal is not bHIDE-THANKSing. If we have code that we want to run after the form has closed, we can’t structure it like the code above.
    So, what to do?

    Fortunately, FireMonkey has an alternative pattern, one that let’s us solve both these issues. Further, it works across all the platforms that FireMonkey supports, so we can still have one set of source. We just have to learn a new pattern.

    So here’s the same code above following the new pattern:

    Code:
    procedure TfrmParent.Button1Click(Sender: TObject);
    var
      dlg : TForm1;
    begin
      dlg := TForm1.Create(nil);
      dlg.ShowModal(procedure(ModalResult : TModalResult)
                    begin
                      if ModalResult = mrOK then
                        // do something here
                    end);
    end;

    ShowModal has been overloaded to allow you to pass in an anonymous method that takes a ModalResult as a parameter. This anonymous method will run when the child form has been closed, giving you a chance to check the ModalResult  and do whatever you need to.

    So, that’s fixed half the problem, but where does my form get destroyed?

    You might be tempted to call dlg.DisposeOf inside that anonymous method. Don’t. Well, not unless you want to generate a segfault. During that anonymous method, our child form is still running. If you look in the source, our anonymous method is invoked as part of the ModalResult property’s setter. After our anonymous method runs, the rest of this setter is going to continue, so destroying the object is probably a bad idea.

    What do we do instead? Forms have an OnClose event, where you can specify an Action that should take place after the form is closed. Some code like this will allow the form to finish what it is doing, and then destroy itself.

    Code:
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Action := TCloseAction.caFree;
    end;
    I especially like that this method of freeing a form has been around in the VCL forever (or, at least as long as I can remember). I used to use it to free MDI Child forms back in the nineties. What I don’t like so much is that I’d forgotten all about it. Thanks to Takahashi-san for reminding me.
     
  2. Yiyo
    Offline

    Yiyo DF Senior

    RE:

    [font=Verdana, Arial, sans-serif]Thanks[/font]
     
  3. mato0o
    Offline

    mato0o DF Junior

    Thank you!
     
  4. jozemauro
    Offline

    jozemauro DF Junior

    RE:

    Tua nos.
     
  5. Maher Tannous
    Online

    Maher Tannous DF Junior

    RE:

    thanks
     
  6. renatopandolfo
    Offline

    renatopandolfo DF Member

    I'll test if it works...thaks
     
  7. yhlee304
    Online

    yhlee304 DF Expert DF Gold User

    good shareing!! ^^b
     
  8. leoplusma
    Offline

    leoplusma DF Junior

    thats a great post, well written - ty :)
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block