对于Android平板电脑应用程序,我用2片段。一个在左侧的画面上,一个在右侧。当在合适的片段点击一个按钮,另一个片段港岛线右边片段的顶部加入。这个片段被添加到backstack。
For a android tablet application I use 2 fragments. One on the left side on the screen and one on the right side. When you click on a button at the right fragment, another fragment wil be added on the top of the right fragment. This fragment is added to the backstack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragmentlayout, fragment2);
ft.addToBackStack("Fragment2");
ft.commit();
该backstack现在是:
The backstack is now:
[片段1] - > [片段2]
[fragment 1] -> [fragment 2]
在左侧也是打开的片段在左边的片段(相同fragment2)的顶部,并将其添加到backstack一个按钮。该backstack现在
On the left side is also a button that opens a fragment on top of the left fragment (same as fragment2) and adds it to the backstack. The backstack is now
[片段1] - > [片段2] - > [片段3]
[fragment 1] -> [fragment 2] -> [fragment 3]
在片段2是一个按钮关闭该片段。
On fragment 2 is a button to close that fragment.
getFragmentManager().popBackStack("Fragment2", FragmentManager.POP_BACK_STACK_INCLUSIVE);
现在的问题是,当我只想要关闭[片段2],[3段]也将被销毁。我可以通过调用manualy删除片段
The problem is when I only want to close [fragment 2], [fragment 3] will also be destroyed. I can remove the fragment manualy by calling
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment2).commit();
但backstack仍将
But the backstack will remain
[片段1] - > [(鬼)片段2] - > [片段3]
[fragment 1] -> [(ghost) fragment 2] -> [fragment 3]
所以,你需要preSS回多一次关闭应用程序。
So you need to press back one time more to close the application.
有没有办法只能从backstack删除[片段2],然后将[片段3]在屏幕上?
Is there a way to only remove [fragment 2] from the backstack and leave [fragment 3] on the screen?
您可以简单地让自己的背部栈和覆盖 onBack pressed()
和处理它以任何方式,你认为合适的。我不认为有达到预期的行为的任何其他方式。
You could simply make your own back stack and override onBackPressed()
and handle it in any fashion you see fit. I don't think there is any other way to achieve the desired behavior.
您自己的背部栈可能仅仅是一个的ArrayList
标记为您的片段。
Your own back stack could simply be an ArrayList
of tags for your fragments.