这是在离开深度嵌套堆栈时清理 Fragment 返回堆栈的正确方法吗? [英] Is this the right way to clean-up Fragment back stack when leaving a deeply nested stack?
问题描述
我正在使用 Android 兼容性库来实现片段并扩展了布局示例,以便片段包含一个触发另一个片段的按钮.
I'm using the Android Compatibility library to implement fragments and have extended the layout sample so that a fragment contains a button which fires off another fragment.
在左侧的选择窗格中,我有 5 个可选项目 - A B C D E
.
In the selection pane on the left I have 5 selectable items - A B C D E
.
每个都在详细信息窗格中加载一个片段(通过 FragmentTransaction:replace
) - a b c d e
Each loads up a fragment (via FragmentTransaction:replace
) in the details pane - a b c d e
现在我扩展了片段 e
以包含一个按钮,该按钮也在详细信息窗格中加载另一个片段 e1
.我在片段 e
的 onClick 方法上做了这个,如下所示:
Now I've extended fragment e
to contain a button which loads up another fragment e1
also in the details pane. I've done this on fragment e
's onClick method as follows:
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.details_frag, newFrag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
如果我做出以下选择:
E - e - e1 - D - E
然后片段 e
位于详细信息窗格中.这很好,也是我想要的.但是,如果我此时按下 back
按钮,它什么也不做.我必须单击它两次,因为 e1
仍在堆栈中.此外,单击周围后,我在 onCreateView 中得到了一个空指针异常:
Then fragment e
is in the details pane. This is fine and what I want. However, if I hit the back
button at this point it does nothing. I have to click it twice because e1
is still on the stack. Furthermore after clicking around I got a null pointer exception in onCreateView:
为了解决"这个问题,我在选择 A B C D E
时添加了以下内容:
To 'solve' this problem I added the following whenever A B C D E
is selected:
FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
只是想知道这是否是正确的解决方案,或者我是否应该做一些不同的事情?
Just wondering whether this is the correct solution or whether I should be doing something different?
推荐答案
好吧,根据预期的行为,有几种方法可以解决此问题,但此链接应该为您提供所有最佳解决方案,毫不奇怪来自 Dianne Hackborn
Well there are a few ways to go about this depending on the intended behavior, but this link should give you all the best solutions and not surprisingly is from Dianne Hackborn
http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42
基本上你有以下选择
- 为您的初始返回堆栈状态使用一个名称并使用
FragmentManager.popBackStack(String name,FragmentManager.POP_BACK_STACK_INCLUSIVE)
. - 使用
FragmentManager.getBackStackEntryCount()
/getBackStackEntryAt().getId()
检索返回堆栈上第一个条目的 ID,以及FragmentManager.popBackStack(int id,FragmentManager.POP_BACK_STACK_INCLUSIVE)
. FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
应该弹出整个后退堆栈...我认为文档那是错误的.(实际上我想它只是不包括以下情况你传入POP_BACK_STACK_INCLUSIVE
),
- Use a name for your initial back stack state and use
FragmentManager.popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE)
. - Use
FragmentManager.getBackStackEntryCount()
/getBackStackEntryAt().getId()
to retrieve the ID of the first entry on the back stack, andFragmentManager.popBackStack(int id, FragmentManager.POP_BACK_STACK_INCLUSIVE)
. FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
is supposed to pop the entire back stack... I think the documentation for that is just wrong. (Actually I guess it just doesn't cover the case where you pass inPOP_BACK_STACK_INCLUSIVE
),
这篇关于这是在离开深度嵌套堆栈时清理 Fragment 返回堆栈的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!