清除背部采用堆碎片 [英] Clear back stack using fragments

查看:99
本文介绍了清除背部采用堆碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植我的Andr​​oid应用程序,以蜂窝我才能使用片段做了一次大的重构。在我的previous版本,当我pressed的主页按钮,我以前做的一个ACTIVITY_CLEAR_TOP以重置回堆栈。

I ported my Android app to honeycomb and I did a big refactor in order to use fragments. In my previous version, when I pressed the Home button I used to do a ACTIVITY_CLEAR_TOP in order to reset the back stack.

现在我的应用程序仅仅是与多个片段(fragments)一个活动,所以当我preSS的主页按钮,我只需更换里面的片段之一。我怎样才能清除,而不必使用startActivity与ACTIVITY_CLEAR_TOP标志我的背部栈?

Now my app is just a single Activity with multiple fragmens, so when I press the Home button I just replace one of the fragments inside it. How can I clear my back stack without having to use startActivity with the ACTIVITY_CLEAR_TOP flag?

推荐答案

<一个href="http://stackoverflow.com/questions/5802141/is-this-the-right-way-to-clean-up-fragment-back-stack-when-leaving-a-deeply-neste">I类似这里发布的内容

这是勒夫的回答,从戴安娜Hackborn:

From Joachim's answer, from Dianne Hackborn:

<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42">http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42

我最终只使用:

FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {    
    fm.popBackStack();
}

但也同样使用了类似:

But could equally have used something like:

FragmentManager.popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE)

这将弹出所有状态到已命名的。然后,您只需更换片段,你想要什么

Which will pop all states up to the named one. You can then just replace the fragment with what you want

这篇关于清除背部采用堆碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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