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 ?

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

i fixed the crash, but wanted to know the behaviour. Thank you .
That was a good question though, not really mentionned in the android docs.
Accept the answer if it fits.
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 ! –
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 ?
|
4

Use SharedPreferences instead, or store information in Application class

Comments

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

2 Comments

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 !
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?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.