这是正确的方式留下了深刻嵌套堆栈时清理片段回栈?这是、堆栈、嵌套、片段

2023-09-12 00:40:39 作者:伪装

我使用了Android Compatbility库来实现fragements,并扩展了布局样品,这样一个片段包含一个按钮,触发了另一个片段。

I'm using the Android Compatbility library to implement fragements and have extended the layout sample so that a fragment contains a button which fires off another fragment.

因此​​,在左侧选择窗格中我有5个可选项目 - ABCDE

So in the selection pane on the left I have 5 selectable items - A B C D E

每个负载可达片段(通过FragmentTransaction:更换)在详细信息窗格中 - ABCDE

Each loads up a fragment (via FragmentTransaction:replace) in the details pane - a b c d e

现在我已经扩展fragement'E'包含一个按钮,它加载了另一个片段'E1'也是在详细信息窗格中。我已经在片段的E的onClick方法,做到了这一点,如下所示:

Now I've extended fragement 'e' to contain a button which loads up another fragment 'e1' also in the details pane. I've done this on fragment's '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();

如果我做了以下选择:

电子 - 电子 - E1 - D - è

E - e - e1 - D - E

然后片段'E'是在详细信息窗格中。这是很好,我想要的。但是,如果我打的返回按钮,在这一点上,它什么都不做。我必须点击它两次,因为'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:

要解决这个问题,我增加了以下每当'ABCDE'被选择:

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?

在此先感谢。彼得·

推荐答案

那么有几种方法去这个取决于预期的行为,但这种联系应该给大家最佳的解决方案,而不是令人惊讶的是,从戴安娜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

从本质上讲,你有以​​下几种选择

Essentially you have the following options

使用一个名称为您最初的背部栈的状态和使用 FragmentManager.popBackStack(字符串名称, FragmentManager.POP_BACK_STACK_INCLUSIVE)。 使用 FragmentManager.getBackStackEntryCount() / getBackStackEntryAt()的getId() 检索在背面叠层的第一个条目的ID,和 FragmentManager.popBackStack(INT ID, FragmentManager.POP_BACK_STACK_INCLUSIVE) FragmentManager.popBackStack(空,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, and FragmentManager.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 in POP_BACK_STACK_INCLUSIVE),