清除Android的片段回栈没有弹出? [英] Clear Android Fragment back stack without popping?

查看:185
本文介绍了清除Android的片段回栈没有弹出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含SlidingMenu的活动( https://github.com/jfeinstein10/SlidingMenu ),其中有三个选项​​,我们姑且称之为A,B1,C1。这些对应于我们展示在活动片段。当您选择从SlidingMenu不同的选择,我通过了FragmentManager换上一张新的当前片段。

We have an Activity that contains a SlidingMenu (https://github.com/jfeinstein10/SlidingMenu) in which there's three options, let's call them A, B1, C1. These correspond to Fragments that we show in the Activity. When you select a different option from the SlidingMenu I replace the current fragment with the new one via the FragmentManager.

从片段B1,你可以去两个人,我们姑且称之为B2和B3。在这里,我们要返回键把你从B2 - > B1或B3 - > B1,所以我叫transaction.addToBackStack(空)。如果我们选择从SlidingMenu一个选项,当你在B2或B3,我们要清除后退堆栈,所以我用code。用在这个问题的 这就要求popBackStack(),直到它的明确清除回堆栈使用的片段。

From Fragment B1 you can go to two others, let's call them B2 and B3. Here we want the Back key to take you from B2 -> B1 or from B3 -> B1, so I call transaction.addToBackStack(null). If we select an option from the SlidingMenu when you're on B2 or B3 we want to clear the back stack, so I use code as suggested in this question Clear back stack using fragments which calls popBackStack() until it's clear.

到目前为止好。

从片段C1,你可以去片段C2。作为C1 / C2更多的是主/从详细设计阶段我用

From Fragment C1 you can go to Fragment C2. As C1/C2 are more of a Master/Detail design I use

fragmentTransaction.setCustomAnimations(R.animator.slide_in_from_right,
    R.animator.slide_out_to_left, R.animator.slide_in_from_left,
    R.animator.slide_out_to_right);

添加幻灯片动画,其中C1滑出,以左侧为C2在从右侧滑动,反之亦然。 pressing返回键,而在C2把我们带回到C1,与反向动画,一切都很好。

to add a slide animation where C1 slides out to the left as C2 slides in from the right, and vice versa. Pressing the Back key while on C2 takes us back to C1, with the reverse animation, and all is good.

如果您选择A或B1从SlidingMenu我们popBackStack()来从后面堆摆脱C1,那么​​它滑出C2出来的权利,看起来怪怪的。我想要做的是明确的,但不运行动画后退堆栈,但我不能找到一个方法来做到这一点。我已经打过电话popBackStackImmediate()来代替,但是这似乎并没有任何区别。

If you select A or B1 from the SlidingMenu and we popBackStack() to get rid of C1 from the back stack, then it slides C2 out to the right, which looks weird. What I'd like to do is clear the back stack without running the animation, but I can't find a way to do that. I've tried calling popBackStackImmediate() instead, but that doesn't seem to make any difference.

作为替代我想我能避免调用addToBackStack可言的,而不是手动处理用户pressing通过Activity.onBack pressed()的返回键,但也许有一个解决方案,我只是不能看到了吗?

As an alternative I guess I could avoid calling addToBackStack at all, and instead manually handle the user pressing the Back key via Activity.onBackPressed(), but perhaps there's a solution I just can't see?

推荐答案

您可以尝试跟随,虽然我不熟悉的卸下摆臂()它似乎应该做你想做的:

You could try the follow, though I am unfamiliar with .remove() it seems that it should do what you want:

myFragmentClass myFragC1 = (myFragmentClass) getFragmentManager().findFragmentByTag("theTagYouUsedWhenAddingToBackStack");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(myFragC1);
transaction.commit();

这篇关于清除Android的片段回栈没有弹出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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