如何为具有多个 Fragment 的 Activity 弹出堆栈? [英] How to pop back stack for Activity with multiple Fragments?

查看:21
本文介绍了如何为具有多个 Fragment 的 Activity 弹出堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 Activity,它包含两个 FrameLayouts(我们称它们为 FrameA 和 FrameB),而它们又每个包含一个 Fragment(我们分别称它们为 FragmentA1 和 FragmentB1).现在,我使用类似于以下的代码提交了一系列单独的片段事务......

Assume I have an Activity which contains two FrameLayouts (let's call them FrameA and FrameB) which in turn each contain a Fragment (let's call them FragmentA1 and FragmentB1 respectively). Now, I commit a series of individual fragment transactions using code similar to the following...

getFragmentManager()
 .beginTransaction()
 .replace(frameId, fragment)
 .addToBackStack(null)
 .commit();

...这样我用FragmentA2替换FrameA中的FragmentA1,然后用FragmentB2替换FrameB中的FragmentB1,然后用FragmentA3替换FrameA中的FragmentA2,然后用FragmentB3替换Frame2中的FragmentB2,最终状态看起来像上图(其中只有 FragmentA3 和 FragmentB3 可见).

... such that I replace FragmentA1 in FrameA with FragmentA2, then I replace FragmentB1 in FrameB with FragmentB2, then I replace FragmentA2 in FrameA with FragmentA3, then I replace FragmentB2 in Frame2 with FragmentB3, and the final state looks like the picture above (where only FragmentA3 and FragmentB3 are visible).

如果我正确理解了后退堆栈的工作原理,按后退"将在 FrameA 和 FrameB 之间交错弹出 Fragment(反映我如何添加它们).

If I understood correctly how the back stack works, pressing 'back' will interleave popping of the Fragments between FrameA and FrameB (reflecting how I added them).

有谁知道是否可以有选择地弹出 FrameA 或 FrameB 上的最后一笔交易?(即,如果我按下Pop FrameA",则 FrameA 将从 FragmentA3 转换回 FragmentA2,相反,如果我按下Pop FrameB",则 FrameB 将从 FragmentB3 转换回 FragmentB2)

Does anyone know if it is possible to pop the last transaction on FrameA or FrameB selectively? (i.e. if I pressed 'Pop FrameA' then FrameA would be transitioned back from FragmentA3 to FragmentA2 and, instead, if I pressed 'Pop FrameB' then FrameB would be transitioned back from FragmentB3 to FragmentB2)

补充:我知道我可以使用 FragmentManager.findFragmentById(int framelayoutId) 方法将 Fragment 最后添加到给定的 FrameLayout,但调用 FragmentTransaction.remove(fragment).commit() 只会从 View 中移除 Fragment,不会将 View 转换回之前显示的 Fragment.

Supplement: I know I can get the Fragment last added to a given FrameLayout using the FragmentManager.findFragmentById(int framelayoutId) method, but calling FragmentTransaction.remove(fragment).commit() only removes the Fragment from the View and does not transition the View back to the Fragment it previously displayed.

推荐答案

基本上,不,一个活动只有一个返回堆栈.

Basically, no, there is only one back stack for an activity.

您只需要实现自己的独立后台堆栈.

You will just need to implement your own separate back stacks.

从 Android 4.0(和相关的支持库)开始,有一些 API 可以让这变得相对容易——FragmentTransaction.detach(Fragment) 可以让你将一个片段置于与它在返回堆栈中时相同的状态,并且 FragmentManager.saveFragmentInstanceState(Fragment) 让你走得更远,彻底扔掉 Fragment 对象.并非巧合的是,它们分别用于实现 ViewPager 的 FragmentPagerAdapter 和 FragmentStatePagerAdapter,因此您可以查看它们的代码作为如何使用它们的示例.

As of Android 4.0 (and the associated support library) there are APIs that should make this relatively easy -- FragmentTransaction.detach(Fragment) lets you put a fragment into the same state it is when in the back stack, and FragmentManager.saveFragmentInstanceState(Fragment) lets you go further and completely throw away the Fragment object. Not coincidentally, these are used to implement ViewPager's FragmentPagerAdapter and FragmentStatePagerAdapter, respectively, so you could look at the code for these as an example of how to use them.

这篇关于如何为具有多个 Fragment 的 Activity 弹出堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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