Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android static variable behaviour on application crash

In my application I have Loginactivity. It has a static variable username and it will be assigned with the user enter values of username. Loginactivity launch activity A and A launch B. In A i use the variable Loginactivity.username.

Now due to some bug in B, application crashes. When I press force close, application is restarted and activity A is the current activity. In activity A I am using a static variable Loginactivity.username. I see that after crash this variable is getting its initial value which is empty string "";

Why is it happening like this? Can you explain this behaviour? So when application crashes all the activities in the stack are restarted? I see that oncreate of Loginactivity is not getting called. Then how is the static variable value getting changed ?

like image 591
png Avatar asked Apr 02 '12 05:04

png


2 Answers

Yes, when an application crashes, the jvm for this app is restarted, your classes are reloaded and you lose all static variables as well as instance variables.

The solution is to remove the crash cause. :)

like image 174
Snicolas Avatar answered Oct 07 '22 00:10

Snicolas


Use SharedPreferences instead, or store information in Application class

like image 25
Dmitry Zaytsev Avatar answered Oct 06 '22 23:10

Dmitry Zaytsev