【问题标题】:Add fragment into backstack behind the topmost/showing fragment将片段添加到最顶部/显示片段后面的后堆栈中
【发布时间】:2014-11-03 08:44:37
【问题描述】:

我想知道是否有办法将片段添加到显示片段后面的后台堆栈中。这样当用户按下“返回按钮”时,添加的片段就会显示给用户。

【问题讨论】:

  • FragmentManager 不包含此类方法。为什么当前关闭时无法捕捉 onBackPressed 并显示所需的片段?

标签: android android-fragments


【解决方案1】:

据我所知,我不确定Fragment Manager 可以做这样的事情!

Step-1:将片段挂载到容器时Why don't you add the fragment tobackStack`

Step-2:接下来onBackpressedclick of any view只需从容器中弹出片段


Here is a sample from another Stackoverflow answer

fragmentTransaction.addToBackStack("fragB");
fragmentTransaction.addToBackStack("fragC");
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);
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2020-11-07
    相关资源
    最近更新 更多