List-object disappear in Process_Globals

cutstone

Member
Licensed User
Longtime User
This might by a silly question, but to me it is about understanding Android Life cycle correctly – still learning.

Is it possible to have a List-object in Process_Globals and assume it is not killed by Android? Or have I missed some documentation somewhere (I have searched for it, before I wrote this).

In the enclosed application, I have made an example that shows my point. I have two activities, one main which contains a string and a list variable in Process_Globals, and another that refers to the two variables. If I launch the application and press the button on the screen, I can display both string and list on the view. However, if I launch the application, and then switch to a task manager and kill the process, return to my application and press the button, only the string is present. My list-object is not initializing anymore.
 

Attachments

  • ListInProcessGlobals.zip
    7.2 KB · Views: 148

cutstone

Member
Licensed User
Longtime User
Variables in process globals remain in memory as long as the process(not activity) remains in memory.
To initialize items in process globals, you should do in the 'FirstTime' of the Main activity Activity_Create.

Well, that's what I have read too. In my enclosed example the string variable remains in memory, but the list-object disappears. I do initialize the object in Activity_Create as you can see.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Well, that's what I have read too. In my enclosed example the string variable remains in memory, but the list-object disappears. I do initialize the object in Activity_Create as you can see.
The string is actually initialized in Process_Globals, hence it will be in memory every time you start the app.

For strMessage, use:
B4X:
if FirstTime then
strMessage.Initialize
   Select strLanguage
      Case "en"
         strMessage.Add("Hello ")
         strMessage.Add("World")
         
      Case "da"
         strMessage.Add("Goddag ")
         strMessage.Add("verden")
   End Select
end if
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
if I launch the application, and then switch to a task manager and kill the process, return to my application and press the button, only the string is present. My list-object is not initializing anymore.
How do you return to you application after you kill the process?
Shouldnt that Main activity be run first?
 
Upvote 0

cutstone

Member
Licensed User
Longtime User
You are correct that test on FirstTime will ensure that the object is not initialized twice… but I cannot see how this solves problem I’m pointing at here. My problem is that the List Object is not even initialized after killing the process, however the string is.

According to this discussion (http://www.b4x.com/forum/basic4andr...19970-test-application-deleting-activity.html), I use a task manager to kill my process, for testing if my code recovers correctly. I’m coding on a larger program, which gave some users an error once in a while. I could not find the bug, until I realize that List objects are remove from the system, when Android kills background activities. So my example here is just to show how to recreate this issue.

I start the program (don’t press the button on the screen), switch to a task manager, kill the process to simulate what Android does when low on memory, hold the home button – select my application, it shows my activity two (SayHello), then press the button. From the log, I can see that the string variable is still present, but the List Object are gone… is that strange? I cannot understand how this happens.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
it shows my activity two (SayHello), then press the button. From the log, I can see that the string variable is still present, but the List Object are gone… is that strange? I cannot understand how this happens.
There lies your problem. When you resume your app it starts from Activity two (SayHello) instead of the Main activity (which is where you initialize your List).

When you 'resume' your app it first runs all the code in Process_Globals...here your string is initialized. Therefore you get an initialized string.

I think part of the issue is that the task killer although kills the app, does not remove it from the recent apps list, and that too the correct activity.

So unfortunately your app starts on the wrong activity (which by the way is NOT normal operation, when Android kills your process, it will start from the main activity).
 
Upvote 0

cutstone

Member
Licensed User
Longtime User
There lies your problem. When you resume your app it starts from Activity two (SayHello) instead of the Main activity.

You are right… I guess too that’s what happens here. To verify, I moved the string assignment from Process_globals to Activity_create and then I get the error on the string too.

Thanks for being so understanding.
 
Upvote 0
Top