如何从 backstack 弹出片段 [英] How to pop fragment off backstack

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

问题描述

我有一个活动 A,它调用片段 Bf,它调用片段 Cf.我希望在调用 Cf 时将 Bf 放在 backstack 中,以便用户可以导航回它.但是,如果在 Cf 中按下特定按钮,我希望 Bf 从 backstack 中删除.这可能吗?

I have an activity A, which calls fragment Bf, which calls fragment Cf. I want Bf to be placed in the backstack when Cf is called so that users can navigate back to it. However, if a specific button is pressed in Cf, I would like Bf to be removed from the backstack. Is this possible?

我看到有一个 popBackStack() 函数.但是,我对这将如何工作感到有些困惑.使用这个功能安全吗?是否有可能在 backstack 上的 Bf 之后插入来自不同应用程序的活动?

I see that there is a popBackStack() function. However, I am a little confused on how this would work. Is it safe to use this function? Is there any possibility that an activity from a different application would be inserted after Bf on the backstack?

另外,有没有办法改变backstack上片段的savedInstanceState?

Also, is there any way to alter the savedInstanceState of the fragment on the backstack?

我就是不知道如何使用模拟器对 backstack 进行稳健的测试.

I just can't figure out how to do a robust test on the backstack using the emulator.

推荐答案

您可以按名称弹出片段.将片段添加到返回堆栈时,只需为它们命名即可.

You can pop the fragment by name. While adding fragments to the back stack, just give them a name.

fragmentTransaction.addToBackStack("fragB");
fragmentTransaction.addToBackStack("fragC");

然后在 Fragment_C 中,使用名称 ie.. fragB 弹出返回堆栈并包含 POP_BACK_STACK_INCLUSIVE

Then in Fragment_C, pop the back stack using the name ie.. fragB and include POP_BACK_STACK_INCLUSIVE

someButtonInC.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        FragmentManager fm = getActivity()
                .getSupportFragmentManager();
        fm.popBackStack ("fragB", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
});

这篇关于如何从 backstack 弹出片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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