gpt4 book ai didi

android - onSaveInstanceState 没有被调用

转载 作者:行者123 更新时间:2023-12-02 18:16:18 29 4
gpt4 key购买 nike

我有一个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com