与配置更改后错活动的参考嵌套片段 [英] Nested fragment with wrong activity reference after configuration change
问题描述
我终于寻找到了支持库的新的嵌套片段的API 修订11。
I'm finally looking into the new nested fragments APIs in the support library revision 11.
一切它的工作pretty的好,直到我试图用通过嵌套片段举行的活动的参考。 后的结构改变的 childFragment 的似乎没有得到分离并重新连接到新的活动
Everything It worked pretty well till I tried to use the activity reference held by the nested fragments. After a configuration change the childFragment doesn't seem to get detached and re-attached to the new activity.
基本上后的方向改变我的 childFragment 的是不一致的状态从我不能得到正确的活动实例与<一个href="https://developer.android.com/reference/android/support/v4/app/Fragment.html#getActivity%28%29"相对=nofollow> getActivity()。
Basically after an orientation change my childFragment is in an inconsistent state from which I can't get the correct activity instance with getActivity().
我manged得到正确的使用<一个href="https://developer.android.com/reference/android/support/v4/app/Fragment.html#getParentFragment%28%29"相对=nofollow> getParentFragment() .getActivity()和它的作品,但我不认为这是正确的道路要走。
I manged to get the correct one using getParentFragment().getActivity() and it works, but I don't think that's the right way to go.
这里是code口用于添加该片段中的 parentFragment 的第一次,在这之后该片段被自动添加回的 parentFragment 的:
here is the code I use to add the fragment in the parentFragment the first time, after that the fragment is automatically added back to the parentFragment:
public void addChildFragment() {
Fragment f = getFragment().getChildFragmentManager().findFragmentByTag( FRAGMENT_CHILD_TAG );
if (f == null) {
FragmentTransaction ft = getFragment().getChildFragmentManager().beginTransaction();
f = new TrackBrowserFragment();
f.setArguments( getFragment().getArguments() );
ft.add( R.id.fragment_album_detail_child_fragment_layout, f , FRAGMENT_CHILD_TAG );
ft.commit();
}
}
在活动的实例时,该不一致明显导致多个问题,我的片段(结合提供服务,广播接收机等)。 我可能做错了什么,因为我不认为这是一个嵌套的片段的正确行为。
This inconsistent in the activity instance obviously lead to multiple problem with my fragment ( binds with services, broadcast receivers and so on ). I'm probably doing something wrong cause I don't think that this is the correct behavior of a nested fragment.
这样:
我做得不对的code? 这是一个嵌套的片段的预期行为?
Am I doing something wrong with the code? Is this the expected behavior of a nested fragment?
我缺少的东西? 我自己我应该分离/附加了吗?
Am I missing something? Should I detach/attach it by myself?
感谢
推荐答案
我发现至极了这个问题,我是在父片段使用setRetainInstance(真),并且保持了孩子片段被分离。
I found out wich was the problem, i was using setRetainInstance(true) in the parent fragment and that kept the child fragment to be detached.
在我删除了这条线一切正常
After I Removed that line everything works as expected
这篇关于与配置更改后错活动的参考嵌套片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!