onSaveInstanceState和finish() [英] onSaveInstanceState and finish()

查看:181
本文介绍了onSaveInstanceState和finish()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:是否可以使用onSaveInstanceState()方法保存数据,然后在Activity上以及在Activity的下一次启动时调用finish(),以将数据恢复到savedInstanceState中?还是活动"的finish()表示数据不见了?

Short question: Is it possible to save data using onSaveInstanceState() method, then call finish() on Activity and upon next start of the Activity to get the data back in savedInstanceState? Or does finish() of an Activity mean the data are gone?

如果第一个答案是正确的,则我的实现存在一些问题,因为尽管保存了数据,但在onCreate()中却出现了null.如果第二个答案是正确的,我将不得不重新考虑如何将我的活动连接在一起:o)

If first answer is correct, I have some problem in my implementation because I am getting null in onCreate() although the data was saved. If second answer is correct, I will have to re-think how I connect my Activities together :o)

推荐答案

是否可以使用onSaveInstanceState()方法保存数据,然后在Activity上以及在Activity的下一次启动时调用finish()以使数据恢复为saveInstanceState?

Is it possible to save data using onSaveInstanceState() method, then call finish() on Activity and upon next start of the Activity to get the data back in savedInstanceState?

否.

还是Activity的finish()意味着数据不见了?

Or does finish() of an Activity mean the data are gone?

是的.保存的实例状态Bundle用于以下情况:从用户的角度来看,您的活动仍然存在,但是由于技术原因该活动被破坏了:

Yes. The saved instance state Bundle is for cases where, from the user's perspective, your activity is still around, but it is being destroyed for technical reasons:

  • 配置更改(例如屏幕旋转)
  • 流程终止(用户很快返回到您的应用)

如果出于其他原因调用了finish()—您直接调用它,用户按BACK等.那么已保存的实例状态就不再需要,可以丢弃.

If finish() is called for other reasons — you calling it directly, user presses BACK, etc. — then the saved instance state is no longer needed and can be discarded.

因此,已保存的实例状态Bundle用于暂存数据,您希望保留这些暂存数据,但是在面对配置更改和过程终止(例如部分填充的内容)时会很容易丢失表格.

As a result, the saved instance state Bundle is for transient data that you would like to retain but are comfortable with losing in the face of configuration changes and process termination, such as the contents of a partially-filled-in form.

这篇关于onSaveInstanceState和finish()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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