Error creating new screens in Delphi for Android [duplicate]

0

I'm creating an application for Android with Delphi 10 Tokyo and in this application has a particular screen called Screen1 . When I click the open button of my menu on the main screen, calling the screen Screen1 , it opens normally.

If I press another menu button to open a "Screen 2" (for example), I check if there is any other screen open and force it to close before opening Screen 2 .

However, if I press the open button on a screen that is already open, even forcing it to close before attempting to open it again, it will fail.

Running this application in Windows just put a Application.ProcessMessages as shown in the routine below and solved the problem.

But in Android gives error, because it speaks that the screen is already open, even forcing its closing.

How do I fix this problem?

OPEN SCREEN 1 button (same for SCREEN 2 )

procedure TfPrincipal.retMenu1Click(Sender: TObject);
begin
   if not CloseForms(Sender) then Exit;

   Application.CreateForm(TfTela1, fTela1);
end;

OnClose event of SCREEN 1 (same for SCREEN 2 )

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

Routine that forces the closing of already open screens

function TfPrincipal.CloseForms(Sender: TObject): Boolean;
var i: Integer;
begin           
   Result := False;

   try
      // Este código não está garantindo o fechamento das telas no Android

      for i := 0 to Application.ComponentCount -1 do
         if Application.Components[i] is TForm then
            if TForm(Application.Components[i]).ClassType <> TfPrincipal then begin
               TForm(Application.Components[i]).Close;
               Application.ProcessMessages;
            end;

      Result := True;
   except on E: Exception do
      ShowMessage('Erro fechando forms: ' + E.Message);
   end;
end;

[CORRECT ANSWER POSTED LOGO BELOW, DIFFERENT FROM THE AUTHOR'S POST OF THE OTHER QUESTION]

    
asked by anonymous 10.11.2017 / 02:57

1 answer

0

I have already solved and used a different solution than the one posted by the other similar question. I just added Application.RemoveComponent(Application.Components[i]); to my Forms closing function. It now works properly on both Android and Windows.

FIXED FUNCTION:

function TfPrincipal.CloseForms(Sender: TObject): Boolean;
var i: Integer;
begin           
   Result := False;

   try
      for i := 0 to Application.ComponentCount -1 do
         if Application.Components[i] is TForm then
            if TForm(Application.Components[i]).ClassType <> TfPrincipal then begin
               TForm(Application.Components[i]).Close;
               Application.RemoveComponent(Application.Components[i]);  // <<<< ACRESCENTEI ISSO
               Application.ProcessMessages;
            end;

      Result := True;
   except on E: Exception do
      ShowMessage('Erro fechando forms: ' + E.Message);
   end;
end;
    
10.11.2017 / 14:26