onSaveInstanceState 没有被调用
onSaveInstanceState is not getting called
我有一个 activity 可以启动结果代码的各种活动,并且在 onActivityResult
方法中获得结果时它会根据结果代码开始适当的 activity。
onSaveInstanceState
未在 Activity 中调用,该 Activity 是为结果开始的。
例如导航 Activity 开始 Activity A 作为 :
Intent intent = new Intent(this, A.class);
startActivityForResult(intent, Constants.REQUEST_CODE);
然后 A 通过设置结果代码完成,因此 App 将再次重定向到导航 activity 并调用 onActivityResult
方法。
所以我的问题是:为什么 Activity A 的 onSaveInstanceState
没有在完成时被调用并且导航回到导航 Activity?
当 Activity
像按下后退按钮一样自然完成时,不会调用方法 onSaveInstanceState()
。那是你的应用程序本身在破坏 Activity
。仅当 Android OS 预计它可能必须杀死您的 activity 才能回收资源时才会调用该方法。
如果 Activity
实际上被 Android 杀死,OS 将确保您收到对 onRestoreInstanceState()
的调用并传递您使用的相同包在 onSaveInstanceState()
方法中保存 activity 的状态。
来自文档:
This method is called before an activity may be killed so that
when it comes back some time in the future it can restore its state.
For example, if activity B is launched in front of activity A, and at
some point activity A is killed to reclaim resources, activity A will
have a chance to save the current state of its user interface via this
method so that when the user returns to activity A, the state of the
user interface can be restored via onCreate(Bundle)
or
onRestoreInstanceState(Bundle)
.
onSaveInstanceState() 仅在 Activity 被杀死时调用。
我不知道你到底想在那个方法中做什么,但你可能应该将你的代码移动到 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.
我在 "main activity" 的 "drawer menu" 部分遇到了同样的问题,因为我已经覆盖了主 activity 中的 "onSaveInstanceState" 方法但是忘记了对 super.onSaveInstanceState() 的调用(结果,它永远不会调用我的 "drawer menu" 的 "onSaveInstanceState()" 方法,它是这个主要 activity 的一部分)。
换句话说:确保您不会忘记在必要时拨打 "super.onSaveInstanceState()"。
我有一个 activity 可以启动结果代码的各种活动,并且在 onActivityResult
方法中获得结果时它会根据结果代码开始适当的 activity。
onSaveInstanceState
未在 Activity 中调用,该 Activity 是为结果开始的。
例如导航 Activity 开始 Activity A 作为 :
Intent intent = new Intent(this, A.class);
startActivityForResult(intent, Constants.REQUEST_CODE);
然后 A 通过设置结果代码完成,因此 App 将再次重定向到导航 activity 并调用 onActivityResult
方法。
所以我的问题是:为什么 Activity A 的 onSaveInstanceState
没有在完成时被调用并且导航回到导航 Activity?
当 Activity
像按下后退按钮一样自然完成时,不会调用方法 onSaveInstanceState()
。那是你的应用程序本身在破坏 Activity
。仅当 Android OS 预计它可能必须杀死您的 activity 才能回收资源时才会调用该方法。
如果 Activity
实际上被 Android 杀死,OS 将确保您收到对 onRestoreInstanceState()
的调用并传递您使用的相同包在 onSaveInstanceState()
方法中保存 activity 的状态。
来自文档:
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via
onCreate(Bundle)
oronRestoreInstanceState(Bundle)
.
onSaveInstanceState() 仅在 Activity 被杀死时调用。
我不知道你到底想在那个方法中做什么,但你可能应该将你的代码移动到 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.
我在 "main activity" 的 "drawer menu" 部分遇到了同样的问题,因为我已经覆盖了主 activity 中的 "onSaveInstanceState" 方法但是忘记了对 super.onSaveInstanceState() 的调用(结果,它永远不会调用我的 "drawer menu" 的 "onSaveInstanceState()" 方法,它是这个主要 activity 的一部分)。
换句话说:确保您不会忘记在必要时拨打 "super.onSaveInstanceState()"。