嵌套的片段和返回堆栈 [英] Nested Fragments and The Back Stack

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

问题描述

是否与Android的嵌套片段的返回堆栈支持互动?

如果是的话,我究竟做错了什么?我在执行中,后退按钮被完全忽略了我加入这个交易到后面堆栈的事实。我希望这是因为一个问题,嵌套的碎片不会,只是我做一些错误的。

下面code是我的片段之一内并用于交换与任何嵌套片段当前显示一个新的片段:

  MyFragment片段=新MyFragment();
     FragmentTransaction英尺= getChildFragmentManager()的BeginTransaction()。
     ft.setCustomAnimations(R.animator.slide_in_from_right,R.animator.slide_out_left,R.animator.slide_in_from_left,R.animator.slide_out_right);
     ft.addToBackStack(空);
     ft.replace(R.id.myFragmentHolder,片段);
     ft.commit();
 

解决方案

我有同样的问题,我想窝片段,并保持背部堆栈每个嵌套片段。

不过...看来,这种情况下不被V4支持库处理。在FragmentActivity code在图书馆,我可以发现:

 公共无效onBack pressed(){
    如果(!mFragments.popBackStackImmediate()){
        完();
    }
}
 

在mFragments重新presents活动的FragmentManager,但它似乎并没有这个经理人传播的流行,以儿童的经理。 一种解决方法是手动调用popBackStackImmediate()对孩子的经理,像这样从FragmentActivity继承了活动:

 私人片段myFragmentContainer;

    @覆盖
    公共无效onBack pressed(){
            如果(!myFragmentContainer.getChildFragmentManager()。popBackStackImmediate()){
                完(); //或如果需要的话调用popBackStack在容器上
            }
    }
 

有可能是一个更好的方式,更自动化的方式,但我需要的是还好吧。

Does the Back Stack support interaction with nested Fragments in Android?

If it does, what am I doing wrong? In my implementation, the back button is completely ignoring the fact that I added this transaction to the back stack. I'm hoping it is not because of an issue with nested fragments and just me doing something incorrectly.

The following code is inside of one of my fragments and is used to swap a new fragment with whatever nested fragment is currently showing:

     MyFragment fragment = new MyFragment();
     FragmentTransaction ft = getChildFragmentManager().beginTransaction();
     ft.setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_left, R.animator.slide_in_from_left, R.animator.slide_out_right);
     ft.addToBackStack(null);
     ft.replace(R.id.myFragmentHolder, fragment);
     ft.commit();

解决方案

I have the same problem, I would like to nest fragments, and to keep a back stack for each nested fragment.

But... it seems that this case is not handled by the v4 support library. In the FragmentActivity code in the library, I can find :

public void onBackPressed() {
    if (!mFragments.popBackStackImmediate()) {
        finish();
    }
}

The mFragments represents the FragmentManager of the activity, but it does not seem this manager "propagates" the pop to children managers. A workaround would be to manually call the popBackStackImmediate() on the child manager, like this in the activity inherited from FragmentActivity :

private Fragment myFragmentContainer;

    @Override
    public void onBackPressed() {
            if (!myFragmentContainer.getChildFragmentManager().popBackStackImmediate()) {
                finish(); //or call the popBackStack on the container if necessary
            }
    }

There might be a better way, and a more automated way, but for my needs it is allright.

这篇关于嵌套的片段和返回堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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