Recent

Author Topic: Android:call one form from another  (Read 1249 times)

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 268
Android:call one form from another
« on: August 19, 2025, 03:49:29 pm »
Please what is the proper way that one form can be called and shown on certain user action,  do something in the called form and then go back to the form that called it ?
One more: Is there a way that only a procedure or function on form2 can be called from form1 without showing form2 ?

God knows I was messing for two days with DeepSeek and ChatGPT but so far i am getting more and more bullshit. My best result was showing called form over the one that called it, so nothing can be read.

I work under win10, i use Lazarus 3.4 + LAMW, I do application programing with Lazarus for use under Linux and Windows  since 2009 but never for Android so far.
« Last Edit: August 19, 2025, 03:52:06 pm by mirce.vladimirov »

dseligo

  • Hero Member
  • *****
  • Posts: 1625
Re: Android:call one form from another
« Reply #1 on: August 19, 2025, 08:32:16 pm »
My best result was showing called form over the one that called it, so nothing can be read.

You are probably on a good way. Set BackgroundColor property of the form(s).

Lazarinr

  • New member
  • *
  • Posts: 9
Re: Android:call one form from another
« Reply #2 on: September 01, 2025, 02:01:46 am »
1. Add the name of unit2 in Uses:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  3.   cthreads,
  4.   {$ENDIF}{$ENDIF}
  5.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, framelayout, stablayout,
  6.   tablelayout, linearlayout, unit2;

2. To call the form when the user clicks the button:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. begin
  3.    // created the form if not exist
  4.    if AndroidModule2 = nil then gApp.CreateForm(TAndroidModule2,AndroidModule2);
  5.    // Show the form
  6.    AndroidModule2.InitShowing;
  7. end;

3. Executes a procedure that is in form2 without having to open it:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button2Click(Sender: TObject);
  2. begin
  3.    // created the form if not exist
  4.    if AndroidModule2 = nil then gApp.CreateForm(TAndroidModule2,AndroidModule2);
  5.    // Excute procedure
  6.    AndroidModule2.ProcedureOfForm2;
  7. end;

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 268
Re: Android:call one form from another
« Reply #3 on: November 26, 2025, 03:42:07 pm »
This is what i got, in "uses" :

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  3.   cthreads,
  4.   {$ENDIF}{$ENDIF}
  5.   Classes, SysUtils, AndroidWidget, Laz_And_Controls,   ZConnection, ZDataset,  And_jni,
  6.  
  7.   framelayout, stablayout, tablelayout, linearlayout ,
  8. unit2   ;
  9.  


and then when I call AndroidModule2 : 

Code: Pascal  [Select][+][-]
  1.   if AndroidModule2 = nil then
  2.      gApp.CreateForm(TAndroidModule2, AndroidModule2);
  3.   AndroidModule2.MoveTaskToFront();
  4.   AndroidModule2.BackgroundColor:=colbrWhite;
  5.   AndroidModule2.InitShowing;    
  6.  


And then the called module (form) is shown.
From that one , i want to go back, exti from AndroidMdule2 to AndroidModule1 :
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule2.Button1Click(Sender: TObject);
  2. begin
  3.   self.Finish;
  4. end;


Called form2 is displayed, works as expected.
When i press the button to go back to form1 it takes me back, as it is expected (the line with self.finish).

Now if i want to go to form2 again it crashes and gets out of the application.
« Last Edit: November 26, 2025, 03:44:03 pm by mirce.vladimirov »

dseligo

  • Hero Member
  • *****
  • Posts: 1625
Re: Android:call one form from another
« Reply #4 on: November 26, 2025, 11:54:01 pm »
and then when I call AndroidModule2 :

Code: Pascal  [Select][+][-]
  1.   if AndroidModule2 = nil then
  2.      gApp.CreateForm(TAndroidModule2, AndroidModule2);
  3.   AndroidModule2.MoveTaskToFront();
  4.   AndroidModule2.BackgroundColor:=colbrWhite;
  5.   AndroidModule2.InitShowing;
  6.  

I do it like this:
Code: Pascal  [Select][+][-]
  1.   If AndroidModule2 = nil then
  2.   begin
  3.     gApp.CreateForm(TAndroidModule2, AndroidModule2);
  4.     AndroidModule2.BackgroundColor := colbrWhite;
  5.     // AndroidModule2.DoJNIPromptOnInit := False;
  6.     AndroidModule2.Init;
  7.   end
  8.   else
  9.     If AndroidModule2.FormIndex = -1 then
  10.       AndroidModule2.ReInit;
  11.  
  12.   AndroidModule2.Show;

Quote
From that one , i want to go back, exti from AndroidMdule2 to AndroidModule1 :
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule2.Button1Click(Sender: TObject);
  2. begin
  3.   self.Finish;
  4. end;

I think you just need to use 'Close' instead of 'Finish'.
'Finish' frees form but doesn't set variable AndroidModule2 to nil. Because of that, when you want to show your form, you don't create it again and thus an exception occurs.

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 268
Re: Android:call one form from another
« Reply #5 on: November 27, 2025, 09:40:41 am »
I copied your advice, now it goes back to Form1 but not goes into Form2 again.
Does not crash but just wont go to Form2 again.

I tried to use self.close but with that it crashes.
On the other hand, self.finish goes back to Form1 but cant return to Form2 again. 

I need to go back and forth, from Form2 call Form3... and ocasionaly, just execute a procedure or function from a form and not show it.

 

and then when I call AndroidModule2 :

Code: Pascal  [Select][+][-]
  1.   if AndroidModule2 = nil then
  2.      gApp.CreateForm(TAndroidModule2, AndroidModule2);
  3.   AndroidModule2.MoveTaskToFront();
  4.   AndroidModule2.BackgroundColor:=colbrWhite;
  5.   AndroidModule2.InitShowing;
  6.  

I do it like this:
Code: Pascal  [Select][+][-]
  1.   If AndroidModule2 = nil then
  2.   begin
  3.     gApp.CreateForm(TAndroidModule2, AndroidModule2);
  4.     AndroidModule2.BackgroundColor := colbrWhite;
  5.     // AndroidModule2.DoJNIPromptOnInit := False;
  6.     AndroidModule2.Init;
  7.   end
  8.   else
  9.     If AndroidModule2.FormIndex = -1 then
  10.       AndroidModule2.ReInit;
  11.  
  12.   AndroidModule2.Show;

Quote
From that one , i want to go back, exti from AndroidMdule2 to AndroidModule1 :
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule2.Button1Click(Sender: TObject);
  2. begin
  3.   self.Finish;
  4. end;

I think you just need to use 'Close' instead of 'Finish'.
'Finish' frees form but doesn't set variable AndroidModule2 to nil. Because of that, when you want to show your form, you don't create it again and thus an exception occurs.

dseligo

  • Hero Member
  • *****
  • Posts: 1625
Re: Android:call one form from another
« Reply #6 on: November 28, 2025, 01:30:24 am »
I copied your advice, now it goes back to Form1 but not goes into Form2 again.
Does not crash but just wont go to Form2 again.

I tried to use self.close but with that it crashes.
On the other hand, self.finish goes back to Form1 but cant return to Form2 again. 

I need to go back and forth, from Form2 call Form3... and ocasionaly, just execute a procedure or function from a form and not show it.

I created test app with 3 forms. On each form there are buttons to show other two forms and 'Close' button to close form. I tested it and it works on my phone (Samsung Galaxy S23).
I attached this example.

P.S.: Other two forms are created at start and they are not freed while app is active, so it is safe to call methods in their classes at any time.
« Last Edit: November 28, 2025, 01:32:14 am by dseligo »

 

TinyPortal © 2005-2018