我需要恢复所有变量onResume? [英] do I need to restore all variables onResume?

查看:136
本文介绍了我需要恢复所有变量onResume?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不好的经验与静态类变量,因为上课的时候​​卸载它们的值都将丢失。所以我尽量避免他们产品总数。

现在我(可能是过度)担心,甚至与正常的变量。

  

我不知道,如果他们的价值也可能会丢失某些   情况就像中断的调用,低内存或其他任何东西。

我可以依靠的变量保持它们的值100%?

我保证某种有效的恢复对所有活动的变量?

谢谢!

解决方案
  

我有不好的经验与静态类变量,因为上课的时候​​卸载它们的值都将丢失。

类没有卸载。 <一href="http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#Processes">Your进程将被终止一段时间后,你有什么在前台,当机器人需要回收内存。

  

我可以依靠的变量保持它们的值100%?还是我保证某种适用于所有活动变量恢复?

活动的通知,当他们被移出前台通过调用的onPause的()。从该活动的角度来看,在的onPause(),直到(可能)对应的 onResume(),在任何时候过程可以被终止,活性丢失。

您需要坐下来,想想你的数据模型。假设用户离开您的应用程序(例如,presses HOME),不回你的应用程序一个小时,或一天,或一个月。需要用户将合理地希望做的那一段时间的任何数据被保存在的持久性数据存储的,如数据库或平面文件。这是你的工作,以确定何时该数据被保存 - 这也许是当用户presses保存按钮,或者它是在的onPause()的活动中,或者是在其他时间。

这是联系在一起的屏幕的当前内容,但并不需要保存一个月没有数据,可以通过的onSaveInstanceState被扶住()。希望你已经在使用该处理屏幕旋转。如果是的话,如果用户离开你的活动,但在时尚由他们可能会通过BACK按钮导航回到它(例如,一个电话进来,然后一条短信进来,然后他们点击了一个链接文本消息,并打开Web浏览器,后来又收回所有的方式回到您的应用程序,它已经终止,在此期间),已保存的实例状态将会恢复。

其他的一切 - 实例数据的活动,或静态数据成员,或任何成员 - 如果用户离开应用程序,Android是否选择以终止过程中可能会丢失。因此,静态数据成员通常只用于短期缓存或事情并不重要,如果他们或丢失用户presses回家时,需要一个电话或者别的什么东西。

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!

解决方案

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.

这篇关于我需要恢复所有变量onResume?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆