- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Activity ,它启动结果代码的各种 Activity ,并在 onActivityResult
方法中获取结果时,它根据结果代码启动适当的 Activity 。
onSaveInstanceState
未在为结果而启动的 Activity 中被调用。
例如,导航 Activity 将 Activity A 启动为:
Intent intent = new Intent(this, A.class);
startActivityForResult(intent, Constants.REQUEST_CODE);
然后 A 通过设置结果代码来完成,以便应用程序将再次重定向到导航 Activity ,并调用 onActivityResult
方法。
所以我的问题是:为什么 Activity A 的 onSaveInstanceState
在完成并导航回导航 Activity 时没有被调用?
最佳答案
仅当 Activity 被终止时才会调用 onSaveInstanceState()。
我不知道您到底想在该方法中做什么,但您可能应该将代码移至 Activity Lifecycle 的相应方法中。
来自 http://developer.android.com/reference/android/app/Activity.html :
Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
还有method description for onSaveInstanceState()准确描述您的情况:
Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.
关于android - onSaveInstanceState 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28211306/
我是 Android 开发新手。我正在使用 onSaveInstanceState() 的覆盖版本来保存我自己的应用程序数据。我注意到我没有在我的函数中调用 super.onSaveInstanceS
如果我将在不调用 super.onSaveInstanceState(outState) 的情况下使用此代码; @Override protected void onSaveInstanceState
我在 fragment 兼容性包示例之后创建了一个 fragment 选项卡小部件,我在 onTabChanged 中更改了 fragment ,但我有一些市场报告在 onSaveInstanceSt
当我离开我的 Activity 时,为什么我的应用程序崩溃了?更具体地说:在我实现 onSaveInstanceState() 之前一切正常和 onRestoreInstanceState() 这是代
我有一个 Activity ,它启动结果代码的各种 Activity ,并在 onActivityResult 方法中获取结果时,它根据结果代码启动适当的 Activity 。 onSaveInsta
我在 onSaveInstanceState 上保存当前进度条时遇到问题。我保存当前正在进行的时间,但是当退出 Activity 并返回时恢复进度不显示剩余时间。 该类显示进度条 public cla
我想实现什么目标? 我想保存 x、y 和 score 的值,因此当用户旋转屏幕时,会检索这些值,而不是重获新生。 我有什么? 可能是正确的 onSaveInstanceState 代码,但由于某些 @
所以我了解了如何使用的主要想法 protected void onSaveInstanceState (Bundle outState) http://developer.android.com/
考虑以下代码,它将静态变量:mMyArray 保存在 onSaveInstanceState 中,并在 onRestoreInstanceState 中恢复它,但是我发现它根本无法保存 int 数组。
当我的应用程序变得复杂并且主要 Activity 开始在内存中保存大量数据(响应用户操作从文件加载和解析)时,我(错误地)从 onSaveInstanceState 切换到 onRetainNonCo
我是我应用的 onSaveInstanceState 实现。我已经成功实现了我的 onSaveInstanceState,但它不会在方向更改时保持当前问题。以下是我的 MainActivity。我一直
我正在尝试在单击后退按钮时保存和检索我的 Activity 的 onSaveInstanceState()。我读过默认情况下它不会保存,因为假设用户在单击返回时已完成 Activity 。但是,在我的
我已经阅读了android文档中的以下声明 Because onSaveInstanceState() is not guaranteed to be called, you should use i
我有几个扩展 LinearLayout 的自定义组件。 现在我想在屏幕方向改变时保存/恢复它们的状态,但我不能通过 Activity 的 onSaveInstanceState/onRestoreIn
我正在尝试了解生命周期方法和 onSaveInstanceState()。 我实际上只需要担心 onCreate() 和 onPause() 是否正确? onCreate()? 我可以/应该忽略 on
我是 Android 开发的新手,我有一个关于 onSaveInstanceState() 的问题。我目前正在处理一个应用程序的登录 Activity 。为了检查用户是否可以登录他们的帐户,我对服务器
我正在编写一个程序,我在同一个 Activity 中调用多个布局,但后来我注意到当我切换布局时,切换之前所做的更改不会恢复并且 onSavedInstanceState(Bundle outState
当 android 由于系统限制而杀死您的进程时,它使您能够通过将数据存储在一个包中来跨进程持久保存数据。如果您的进程被终止,这个包保存在哪里?它存在于哪个进程中?它住在内存中的什么地方?它存在于内核
我在我的 android Activity 中实现了 onSaveInstanceState 和 onRestoreInstanceState。 这些似乎工作正常,但我想在某些事情发生时(例如,当用户
我的问题似乎与其他人相反。 :) 每当我从一个 Activity 导航到下一个 Activity 时,都会调用我的 onSaveInstanceState。我检查了 LogCat,它绝对不会终止 Ac
我是一名优秀的程序员,十分优秀!