OnActivityCreate and global variables lazarus / 43-Android ---------------------------------------------------------------------- dseligo · 2024-08-01 16:33 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.AndroidModule1ActivityCreate(Sender: TObject; intentData: jObject);begin  If AndroidModule2 = nil then  begin    gApp.CreateForm(TAndroidModule2, AndroidModule2);    AndroidModule2.DoJNIPromptOnInit := False;    AndroidModule2.BackgroundColor := colbrGhostWhite;    AndroidModule2.Init;  end;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 · 2024-08-02 07:22 Crash occurs in jForm.Init (androidwidget.pas), when jForm_Create is called: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure jForm.Init; ...   if FActivityMode <> actEasel then...     FjObject:=  jForm_Create(gApp.Jni.jEnv, gApp.Jni.jThis, Self); {jSef} ... 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 · 2024-08-05 21:57 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AndroidModule2.TextView1.Text := 'test'; // this doesn't crash appAndroidModule2.EditText1.Text := 'test'; // if I do this then app crashes in then following lineAndroidModule2.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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i If AndroidModule2 = nil thenbegin  gApp.CreateForm(TAndroidModule2, AndroidModule2);  AndroidModule2.Init;endelse  If AndroidModule2.FormIndex = -1 then    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 · 2024-08-08 01:13 I'll test this out when I have some time.