编程回到previous片段在backstack [英] Programmatically go back to the previous fragment in the backstack

查看:135
本文介绍了编程回到previous片段在backstack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个具有编程方式添加片段活动:

Say I have an activity that has fragments added programmatically:

private void animateToFragment(Fragment newFragment, String tag) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container, newFragment, tag);
    ft.addToBackStack(null);
    ft.commit();
}

什么是返回到previous片段是可见的最好方法是什么?

What is the best way to return to the previous fragment that was visible?

我发现早在Android的但我按钮点击功能M想模拟一个返回键事件是不正确的方式去了解它(我不能得到它的工作要么):

I found back functionality on button click in android but I'm thinking simulating a back key event isn't the right way to go about it (and I can't get it to work either):

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

调用完成()只是关闭它,我不感兴趣的活动。

Calling finish() just closes the activity which I'm not interested in.

有没有更好的方式来进行此事?

Is there a better way to go about this?

推荐答案

getFragmentManager()。popBackStack()方法(有多种可供选择)

Look at the getFragmentManager().popBackStack() methods (there are several to choose from)

<一个href="http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack">http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

这篇关于编程回到previous片段在backstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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