Skip to content Skip to sidebar Skip to footer

Can Static Variable Value Be Nulled By System In Android App?

I have noticed, that when android OS closes activities which are not in the top of activity stack, some of my static variables become null. I'm absolutely sure, the variables point

Solution 1:

Android does not close activities which are not on top of the activity stack. If your application goes to the background and Android decides that it wants to reclaim the memory it just kills the process that hosts your activities. When the user returns to the application, Android creates a new process and recreates the activity that was on the top of the activity stack.

In most probability, that's what you are seeing. Obviously if your process is killed and recreated, your static variables will be null.

Solution 2:

They are only being nulled if the underlying VM/Thread that the activity was running in was killed. then it is like you are completely restarting the application. Don't rely on static variables, if you need to keep something around, store it in a DB or a Preference.

Post a Comment for "Can Static Variable Value Be Nulled By System In Android App?"