使用片段清除返回堆栈 [英] Clear back stack using fragments

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

问题描述

我将我的 Android 应用程序移植到了 Honeycomb,并且为了使用片段我做了一个很大的重构.在我以前的版本中,当我按下主页按钮时,我曾经执行一个 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.

现在我的应用程序只是一个带有多个片段的活动,所以当我按下主页按钮时,我只是替换其中的一个片段.如何在不必使用带有 ACTIVITY_CLEAR_TOP 标志的 startActivity 的情况下清除我的后台堆栈?

Now my app is just a single Activity with multiple fragments, 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?

推荐答案

我在这里发布了类似的内容

来自 Joachim 的回答,来自 Dianne Hackborn:

From Joachim's answer, from Dianne Hackborn:

http://groups.google.com/group/android-开发人员/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:

((AppCompatActivity)getContext()).getSupportFragmentManager().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天全站免登陆