的onSaveInstanceState是没有得到所谓的 [英] onSaveInstanceState is not getting called

查看:250
本文介绍了的onSaveInstanceState是没有得到所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动的结果codeS和对 onActivityResult 方法得到结果的各种活动活动启动基于结果code适当的活动。

I have an activity which starts various activities for result codes and on getting results in onActivityResult method it starts appropriate activity based on result code.

的onSaveInstanceState 是没有得到所谓的在开始为结果的活动。

onSaveInstanceState is not getting called in Activity which started for result.

例如导航活动开始活动一为:

For example Navigation Activity starts Activity A as :

Intent intent = new Intent(this, A.class);
    startActivityForResult(intent, Constants.REQUEST_CODE);

然后A完成设置结果code所以应用程序会重定向到航海活动再次 onActivityResult 方法调用。

所以我的问题是:为什么活动A的的onSaveInstanceState 是没有得到所谓的光洁度和导航返回导航活动

So my question is: Why Activity A's onSaveInstanceState is not getting called at finish and navigation back to Navigation Activity ?

推荐答案

的onSaveInstanceState()如果活动被杀害时,才会调用。

onSaveInstanceState() is only called if the Activity is being killed.

我不知道你想做的方法究竟是什么,但你应该将你的code到的活动生命周期

I don't know what exactly you want to do in that method, but you probably should move your code to the corresponding methods of the Activity Lifecycle.

http://developer.android.com/reference/android/app /Activity.html

请注意保存永久数据的onPause(这很重要),而不是的onSaveInstanceState(捆绑),因为后者并不是生命周期回调的一部分,所以不会被调用为它的文档中描述的每一种情况。

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.

另外,<一个href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)"相对=nofollow>方法描述的onSaveInstanceState()准确地描述你的情况:

Also the method description for onSaveInstanceState() describes exactly your situation:

不要将此方法与活动的生命周期回调,如在onPause(),当一个活动被放置在背景或在它的方式来破坏它总是叫或的onStop(),这是破坏之前调用。没有必要调用的onSaveInstanceState(软件包)上B,因为该特定实例将永远不会被恢复:当用户导航回到从活动B至活性的时的onPause()和的onStop()被调用的一个例子而不是本方法是的,所以系统避免调用它。当的onPause()被调用,而不是的onSaveInstanceState一个例子(包)是当活动中的B活性的前启动:该系统可避免活动主叫的onSaveInstanceState(捆绑)如果不是的B,因为一生中丧生A的用户界面的状态将保持不变。

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.

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

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