Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

android - do I need to restore all variables onResume?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...