I had bad experience with static class variables
since their values are lost when the class unloads. Therefore I avoid them alltogether.
Now I am (probably overly) worried even with "normal" variables.
I'm not sure if their value also might get lost in certain circumstances like disruptions by a call, low memory or anything else.
Can I rely on the variables hold their values 100% ? or
do I ensure some kind of valid restore for all activity variables?
Thanks!
onResume(): Resume an activity onResume() is called at the start of the active lifetime; more specifically, it is called at the start of the active lifetime; more specifically it is called after onRestoreInstanceState(Bundle), onRestart(), or onPause() for your activity to start interacting with the user.
onStart() , onStop() , onRestart() : Called back when the Activity is starting, stopping and re-starting. onPause() , onResume() : Called when the Activity is leaving the foreground and back to the foreground, can be used to release resources or initialize states.
onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .
I had bad experience with static class variables since their values are lost when the class unloads.
Classes do not "unload". Your process will be terminated sometime after you have nothing in the foreground, when Android needs to reclaim memory.
Can I rely on the variables hold their values 100% ? or do I ensure some kind of valid restore for all activity variables?
Activities are notified of when they are moved off the foreground by a call to onPause()
. From the standpoint of that activity, any time after onPause()
until (possibly) a corresponding onResume()
, the process may be terminated and the activity be lost.
You need to sit back and think about your data model. Suppose the user leaves your app (e.g., presses HOME) and does not return to your app for an hour, or a day, or a month. Any data that the user would reasonably expect to stick around for that period of time needs to be saved in a persistent data store, such as a database or flat file. It is your job to determine when that data gets saved -- perhaps it is when the user presses a Save button, or perhaps it is in onPause()
of an activity, or perhaps it is at some other time.
Data that is tied to the current contents of the screen, but does not need to be saved for a month of absence, can be held onto via onSaveInstanceState()
. Hopefully you are already using this to handle screen rotations. If so, and if the user leaves your activity but in a fashion by which they might navigate back to it via the BACK button (e.g., a phone call comes in, then a text message comes in, then they click on a link in a text message and bring up the Web browser, and later BACK all the way back to your app, which had been terminated in the meantime), your saved instance state will be restored.
Everything else -- instance data members of an activity, or static data members, or whatever -- may get lost if the user leaves the app, if Android elects to terminate your process. Hence, static data members are typically only used for short-term caches or for things that do not matter if they are lost when the user presses HOME or takes a phone call or whatever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With