在交易片段backstack错误更换片段时? [英] Fragment backstack bug when replacing fragments in a transaction?

查看:109
本文介绍了在交易片段backstack错误更换片段时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是导致问题的方案:

Here's the scenario that causes problems:

  1. 在我启动活性的研究与将举行presented一个片段的ViewGroup。
  2. 在我打开片段A到这个ViewGroup中通过调用.replace(),因为我救到backstack交易。
  3. 在我打开片段B到ViewGroup中,替换片段A.同样,我保存了交易。
  4. 我旋转设备的两倍。
  5. 在第二旋转,片段A(这是不可见的时刻),将抛出一个空指针异常。

这异常是由片段A抛出,因为我在它的onSaveInstanceState()方法储蓄的看法的某些值(EditTexts例)。看来,这对第二旋转时,系统不会实例这些意见,所以当我问他们的价值观,我得到一个空指针异常。可以这样避免不知何故?或正在使用.replace操作中保存到backstack片段transcation轻率?

This exception is thrown by Fragment A, because I'm saving some values of Views (EditTexts e.g.) in its onSaveInstanceState() method. It seems, that on the second rotation, the system doesn't instantiate these Views, so when I'm asking their values, I get a NullPointer exception. Can this be avoided somehow? Or is using .replace operations in a fragment transcation saved onto the backstack unadvised?

推荐答案

我有这个,但不能完全记得我做过什么来解决,但总的来讲的细节(和道歉大脑转储)我做的以下内容:

I've had this but can't quite recollect the specifics of what I did to fix but generally speaking (and apologies for the brain dump) I do the following:

  1. 呼叫 setRetainInstance(真)的onCreate 来避免碎片娱乐

保存的编辑控件等国家在的onSaveInstanceState 来使用,如果活动被杀害,你会得到恢复与非空包(你不应该'吨得到在一个方向变化1非空包。)

Save the state of edit controls etc. in onSaveInstanceState to be used if activity is killed and you get restored with a non-null bundle (you shouldn't get a non-null bundle on an orientation change with 1.)

维护成员变量的编辑控制值(该片段是不会重新创建),确保他们得到在 onDestroyView 从编辑控件,然后更新用它们来恢复 onCreateView

Maintain edit control values in member variables (as the fragment is not going to be recreated) ensuring they get updated in an onDestroyView from the edit controls and then use them to restore the edit control values in onCreateView

您也可以在 onDestroyView 我在 onCreateView 设置为true一个标志和虚假的,并确保我不T触摸UI控件时认为不在身边。

Have a flag which I set to true in onCreateView and false in onDestroyView and ensure I don't touch UI controls when the view is not around.

BTW使用替换同时加入了交易后退堆栈是完全确定。

BTW Using replace while adding the transaction to the back stack is perfectly OK.

希望有什么东西在那里帮助。彼得。

Hope there's something in there that helps. Peter.

这篇关于在交易片段backstack错误更换片段时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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