Error creating new screens in Delphi for Android

Asked

Viewed 328 times

0

I’m creating an app for Android with Delphi 10 Tokyo and in this app has a certain screen, called Tela1. When I click the Open button from my menu on the main screen, calling the screen Tela1, opens normally.

If I press another menu button in order to open a "Screen 2" (for example), I make a check if there is any other screen open and force its closure before opening the Screen 2.

However, if I press the opening button of a screen that is already open, even forcing its closure before trying to open it again, it will give error.

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

But in the Android error, because it says that the screen is already open, even forcing its closure.

How do I fix this problem?

Knob OPEN SCREEN 1 (equal to the SCREEN 2)

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

   Application.CreateForm(TfTela1, fTela1);
end;

Event Onclose of SCREEN 1 (equal to the SCREEN 2)

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

Routine that forces the closure of screens already open

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 RIGHT BELOW, DIFFERENT FROM THE AUTHOR’S POST OF THE OTHER QUESTION]

  • It really seems to be the same problem, but as Andrey commented in the answer that the author himself took for granted, is so confused that it is not possible to understand well what was done. The impression is that a gambiarra was made... In my problem, the impression it gives is that it only lacks a routine that runs Processmessages properly on Android too, because without it gives the same problem on Windows likewise.

1 answer

0


I have already solved and used a solution different from the one posted by the author of the other similar question. I just added Application.RemoveComponent(Application.Components[i]); to my Forms closing function. Now it works correctly on both Android and Windows.

CORRECTED 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.