11

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 ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
png
  • 4,368
  • 7
  • 69
  • 118

4 Answers4

16

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. :)

xpda
  • 15,585
  • 8
  • 51
  • 82
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • i fixed the crash, but wanted to know the behaviour. Thank you . – png Apr 02 '12 at 06:04
  • 4
    That was a good question though, not really mentionned in the android docs. – Snicolas Apr 02 '12 at 06:05
  • Accept the answer if it fits. – Snicolas Apr 02 '12 at 06:39
  • If application is restarted , how is it getting the previous activity stack. My application whenever restarted in the Loginactivity i read username from sharedpref. Here i see that Loginactivity is not getting restared, instaed activity A which is on top of stack is launched. And in A i set variable as Loginactivity.username and this variable is reset though activity not restared ! – – png Apr 02 '12 at 06:58
  • So reading the shared preferences doesn't happen at the right place. You should consider reading in every activity that needs the value. But did you correct the crash cause ? – Snicolas Apr 02 '12 at 07:03
  • This is a good answer. But... Of course we want to fix all the crashes. But in my case i wanted to send WS with some data on crash. Made a service with the http request logic. But in crash the variables i inited in the service was null. Still looking for way to preserve them. – Moshe Yamini Nov 11 '20 at 12:26
3

Use SharedPreferences instead, or store information in Application class

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
3

When your Activity B crashes the Android Dalvik Virtual Machine that your application runs in (each app has its own DVM which are separate processes) is killed. When you are starting Activity A the username is "" because Java by defaults sets all instance variable (class variable or fields if you like) to null (references), 0 (primitives), and "" for strings. So your Activity A is working correctly. You just need to either store the username in the shared preferences, a database, or trigger the event for the user to login again... I would also fix Activity B... Haha

jjNford
  • 5,170
  • 7
  • 40
  • 64
  • I have a doubt here : If application is restarted , how is it getting the previous activity stack. My application whenever restarted in the Loginactivity i read username from sharedpref. Her i see that Loginactivity is not getting restared, instaed activity A which is on top of stack is launched. And in A i set variable as Loginactivity.username and this variable is reset though activity not restared ! – png Apr 02 '12 at 06:57
  • Perhaps your shared prefs are not working correctly. Your stack was killed with the VM. Or your logic to bypass the login activity has a glitch. Source? – jjNford Apr 02 '12 at 07:01
1

This is because you have a memory leak caused by this static member you keep in LoginActivity.

I would consider keeping this variable in Application scope (custom application class) or save it into DB.

Anyway, just remember to nullify this variable when your application is done.

MarkySmarky
  • 1,609
  • 14
  • 17