我应该在onCreate或onRestoreInstanceState中恢复savedinstancestate吗? [英] Should I restore savedinstancestate in onCreate or in onRestoreInstanceState?

查看:288
本文介绍了我应该在onCreate或onRestoreInstanceState中恢复savedinstancestate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动会为结果启动其他一些活动,因此,当结果返回时,该活动可能已被破坏或没有被重新创建.

I have an activity that starts some other activities for results, so when the result comes back, the activity may or may not have been destroyed and recreated.

我已重写onSaveInstanceState以便添加需要保留和还原的数据.

I have overridden onSaveInstanceState so as to add the data that needs to be preserved and restored.

当活动被销毁并重新创建时,onCreate传递了savedInstanceState捆绑包;但是 onRestoreInstanceState()会被调用并传递相同的包.

When the activity gets destroyed and recreated, onCreate is passed the savedInstanceState bundle; but also onRestoreInstanceState() is called and passed the same bundle.

那么我应该把从包中提取数据并恢复状态的代码放在哪里?在onCreate或onRestoreInstanceState中?可以保证总是叫后者吗?

So where should I put the code that extracts the data from the bundle and restores the state? In onCreate or in onRestoreInstanceState? Is the latter guaranteed to be always called?

是否可以在不调用onCreate的情况下调用onRestoreInstanceState? (例如,该活动是否已停止并重新启动,但未销毁并重新创建)?

Is it possible that onRestoreInstanceState is called without calling onCreate? (e.g. if the activity gets stopped and restarted but not destroyed and recreated)?

推荐答案

您可以选择实现onRestoreInstanceState(),而不是在onCreate()期间恢复状态,该方法由系统在onStart()方法之后调用.系统调用onRestoreInstanceStateState()仅在存在要还原的保存状态时才使用,因此您无需检查Bundle是否为NULL"

"Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is NULL"

下面的链接非常清楚地说明了重新启动活动.

following link explain pretty clearly about restart activity.

Android指南

这篇关于我应该在onCreate或onRestoreInstanceState中恢复savedinstancestate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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