Recent

Author Topic: OnActivityCreate and global variables  (Read 1009 times)

dseligo

  • Hero Member
  • *****
  • Posts: 1345
OnActivityCreate and global variables
« on: August 01, 2024, 06:33:45 pm »
In what cases is OnActivityCreate called?

I noticed that sometimes is called and global variables are not reset in LAMW.

I.e. I have something like this in OnActivityCreate:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1ActivityCreate(Sender: TObject; intentData: jObject);
  2. begin
  3.   If AndroidModule2 = nil then
  4.   begin
  5.     gApp.CreateForm(TAndroidModule2, AndroidModule2);
  6.     AndroidModule2.DoJNIPromptOnInit := False;
  7.     AndroidModule2.BackgroundColor := colbrGhostWhite;
  8.     AndroidModule2.Init;
  9.   end;
  10. end;

Most of the time this works fine.
But sometimes (when switching apps on phone) OnActivityCreate is called again. Problem is that global variables (like AndroidModule2) retains their values, but there aren't valid any more.
So when later AndroidModule2.Show is called app crashes. Then global variables are reset and everything works.

What I am doing now, is I reset variables in OnActivityCreate. But it is easy to miss something (if I add new form i.e.).

Is there a solution?

dseligo

  • Hero Member
  • *****
  • Posts: 1345
Re: OnActivityCreate and global variables
« Reply #1 on: August 02, 2024, 09:22:08 am »
Crash occurs in jForm.Init (androidwidget.pas), when jForm_Create is called:

Code: Pascal  [Select][+][-]
  1. procedure jForm.Init;
  2.  
  3. ...
  4.  
  5.   if FActivityMode <> actEasel then
  6. ...
  7.  
  8.     FjObject:=  jForm_Create(gApp.Jni.jEnv, gApp.Jni.jThis, Self); {jSef}
  9.  
  10. ...

This occurs only on some forms (and only sometimes when app is started again). I'll try to see what is different on these forms.

dseligo

  • Hero Member
  • *****
  • Posts: 1345
Re: OnActivityCreate and global variables
« Reply #2 on: August 05, 2024, 11:57:08 pm »
I further investigated this.
When app is restarted, created form variables (i.e. AndroidModule2 in above example) are not nil. After calling 'Show' method there is check if FormIndex field is -1. If it is then 'ReInit' method is called, which later calls 'Init', which eventually calls jForm_Create (where this crash occurs).
Problem only manifests if I set jEditText components in the form before 'Show'. If I set jTextView components it all works. Strange.
Code: Pascal  [Select][+][-]
  1. AndroidModule2.TextView1.Text := 'test'; // this doesn't crash app
  2. AndroidModule2.EditText1.Text := 'test'; // if I do this then app crashes in then following line
  3. AndroidModule2.Show;

I don't know why this happens, but following workaround in code where I create form seems to work for me:
Code: Pascal  [Select][+][-]
  1. If AndroidModule2 = nil then
  2. begin
  3.   gApp.CreateForm(TAndroidModule2, AndroidModule2);
  4.   AndroidModule2.Init;
  5. end
  6. else
  7.   If AndroidModule2.FormIndex = -1 then
  8.     AndroidModule2.ReInit;

Hope this helps someone and if somebody has some better solution please say so.

I attached my test app with AndroidManifest.xml.

Guser979

  • Jr. Member
  • **
  • Posts: 63
Re: OnActivityCreate and global variables
« Reply #3 on: August 08, 2024, 03:13:01 am »
I'll test this out when I have some time.

 

TinyPortal © 2005-2018