如何弹出片段关闭backstack弹出、片段、backstack

2023-09-04 03:27:48 作者:隐约期待

我有活性的,它调用片段BF,它调用片段参看我想的Bf被放置在backstack当比照被调用,以便用户可以导航回到它。但是,如果一个特定的按钮被pssed在比照$ P $,我想的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()函数。但是,我对如何做到这一点的工作有点困惑。它是安全的使用这个功能吗?是否有来自不同应用的活动会的Bf之后插入对backstack任何可能性?

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。

Sorry if this is a simple question. 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,使用该名称即.. 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);
        }
    });
 
精彩推荐
图片推荐