对方向变化保存的用户界面 - 的onSaveInstanceState工作不正常,如果保留片段 [英] Saving UI on orientation change - onSaveInstanceState not working as expected if retaining Fragment

查看:114
本文介绍了对方向变化保存的用户界面 - 的onSaveInstanceState工作不正常,如果保留片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用compat的lib中V1(不使用V2 | 3,因为某些缺陷);的变化<一href="http://stackoverflow.com/questions/7320785/onsaveinstancestate-is-not-saving-my-values-oncreate-input-bundle-is-always-nu">this问题。

Using compat lib v1 (not using v2|3 because of certain bugs); a variation of this question.

我有一个片段的UI具有各种控件我想在一个方向变化保持其状态。

I have a fragment whose UI has various controls whose state I want to maintain on an orientation change.

父活动被销毁的方向变化(请不要告诉我明显的变化,以避免活动娱乐!!!!)。

The parent activity is being destroyed on orientation change (PLEASE don't tell me about manifest changes to avoid activity recreation!!!!).

该段调用 setRetainInstance(真)

1)现在,我的理解是,有唯一的ID意见应保持在说,一个方向变化的一些状态。鉴于这一点,我希望一个非空包成 onCreateView | onActivityCreated 但它是空

1) Now my understanding is that Views with unique IDs should retain some state on say an orientation change. Given this I would expect a non-null bundle into onCreateView|onActivityCreated but it is null.

2),如果我保存状态的onSaveInstanceState 任何情况下(保证我称之为超)我仍然可以在onCreateView一个空包| onActivityCreated`

2) In any case if I save state in onSaveInstanceState (ensuring I call super) I still get a null bundle in 'onCreateView|onActivityCreated`

3)如果我不叫 setRetainInstance(真)然后我得到的 onCreateView一个非空包| onActivityCreated 即使我没有一个`的onSaveInstanceState'方法。

3) If I don't call setRetainInstance(true) then I DO get a non-null bundle in onCreateView|onActivityCreated even if I don't have an `onSaveInstanceState' method.

我的,这是按预期工作,我的整个生命周期的理解是坏了的问题?无论如何,我猜着对我来说,最好的办法是保留的片段,然后保持控件的状态自己的片段中。

The questions I have is, is this working as expected and my understanding of the life-cycle is broken? Regardless, I'm guessing that the best way forward for me would be to retain the fragment and then maintain the state of the controls myself within the fragment.

在此先感谢。彼得。

推荐答案

如果您使用 setRetainInstance(真)那当然束为空。该片段不被破坏,但是从目前的活动仅分离并附着到新的活动。只有当片段被破坏你得到你的的onSaveInstanceState 保存值的包。只是删除 setRetainInstance(真)并使用onCreateView()保存的值设置您的自定义视图。

If you use setRetainInstance(true) then of course the bundle is null. The fragment is not destroyed but only detached from the current activity and attached to the new activity. Only when the fragment is destroyed do you get a bundle with the values you saved in onSaveInstanceState. Just remove setRetainInstance(true) and use the saved values in onCreateView() to setup your custom views.

这篇关于对方向变化保存的用户界面 - 的onSaveInstanceState工作不正常,如果保留片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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