本文介绍了如何弹回栈与多个片段活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含两个FrameLayouts的活动(我们姑且称之为FrameA和FrameB),这反过来的每个的包含片段(我们姑且称之为分别FragmentA1和FragmentB1)。现在,我犯了一系列使用code类似于以下...

个别片段的交易

  getFragmentManager()
 .beginTransaction()
 .replace(frameId,片段)
 .addToBackStack(空)
 。承诺();
 

......这样的,我替换FragmentA1在FrameA与FragmentA2,然后我更换FragmentB1在FrameB与FragmentB2,然后我更换FragmentA2在FrameA与FragmentA3,然后我用FragmentB3替换式2 FragmentB2,并最终状态看起来像上面图片中(其中只有FragmentA3和FragmentB3可见)。

如果我理解正确的是如何回栈工作,pressing'回'将交错FrameA和FrameB之间的片段的爆裂(反映了我如何将它们添加)。

有谁知道是否有可能流行的最后一笔交易上FrameA或FrameB选择? (也就是说,如果我pressed'流行FrameA,然后FrameA将转换从FragmentA3到FragmentA2,转而回来,如果我pressed'流行FrameB,然后FrameB将从FragmentB3到FragmentB2转换后)

解决方案

基本上没有,只有一回堆栈的活动。

您只需要实现自己的独立的回栈。

由于Android的4.0(以及相关的支持库)有原料药,应该使这个相对容易的 - FragmentTransaction.detach(Fragment)让你把一个片段到相同的状态,它是在后面栈的时候,和FragmentManager.saveFragmentInstanceState(Fragment)让您更进一步,彻底扔掉片段对象。并非巧合的是,这些被用来实现ViewPager的FragmentPagerAdapter和FragmentStatePagerAdapter分别,所以你可以看看code为这些作为如何使用它们的例子。

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();

... 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).

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).

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)

解决方案

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

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

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.

这篇关于如何弹回栈与多个片段活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:09