当前位置: 首页 > 知识库问答 >
问题:

如何使用子片段管理backbackback?

苏德容
2023-03-14

我对嵌套/子片段有问题。我的用例是:我将Frag A作为父片段,将FragChild1、FragChild2、FragChild3作为子片段显示在FragA中。现在,在FragChild3的背压下,它应该像这样工作:

FragChild3-

我用来添加子片段的代码是-

for ChildFrag1-

Fragment mChildFragment1 = new ChildFragment1();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.container_view, mChildFragment1);
        transaction.addToBackStack("FragChild1");
        transaction.commit();
        
for ChildFrag2-

Fragment mChildFragment2 = new ChildFragment2();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.container_view, mChildFragment2);
        transaction.addToBackStack("FragChild2");
        transaction.commit();

我在StackOverflow中搜索了相关答案,但尚未找到任何适当的答案/方法来管理子片段的backbackback。

共有3个答案

辛麻雀
2023-03-14

将标记传递给replace方法。标记可以像片段名称(字符串)一样简单。更改代码如下:

transaction.replace(R.id.container_view, mChildFragment1, "FragChild1");
transaction.addToBackStack("FragChild1");
transaction.commit();
鱼锦
2023-03-14

现在,可以使用OnBackPressedDispatcher实现此行为,而无需在活动中重写onBackPressed。

在父片段(片段容器所在的位置)中,将此代码添加到onAttach方法中:

override fun onAttach(context: Context) {
    super.onAttach(context)
    val backCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            // Remove all fragments from the childFragmentManager,
            // but exclude the first added child fragment.
            // This child fragment will be deleted with its parent.
            if (childFragmentManager.backStackEntryCount > 1) {
                childFragmentManager.popBackStack()
                return
            }
            // Delete parent fragment
            parentFragmentManager.popBackStack()
        }
    }
    requireActivity().onBackPressedDispatcher.addCallback(this, backCallback)
}

而不是像这样在容器中添加片段:

childFragmentManager.commit {
    replace(R.id.fragmentContainerRoot, fragment)
    addToBackStack(null)
}
章彬郁
2023-03-14

你的代码看起来不错。只需重写包含父片段的活动中的onbackpressed方法,并将给定代码放入其中。

if (parentfragment.getChildFragmentManager().getBackStackEntryCount() > 1) {
    parentfragment.getChildFragmentManager().popBackStackImmediate();
} else {
    super.onBackPressed();
}
 类似资料:
  • 我想管理回压中的所有片段。当我点击后退按钮,然后打开上一个片段并完成当前片段。

  • 我想在收到一些HTTP请求的响应后,更新几个<code>片段 。-- 活动 适配器 碎片样本 日志显示位置0,1处的片段的<code>updateData</code>工作良好。但是对于位置2处的片段,它会导致错误。 我发现的区别是适配器没有为位置2调用,这意味着没有调用的。因此不存在。 我的直觉告诉我,问题一定来自适配器的工作流或fragmentManager事务。但是作为一个Android初学

  • 我已经为我的REST API添加了一个主要的路由段,它是在Node中编写的。js并在Lambda函数上运行。我还为X射线启用了API网关跟踪。我想将sql查询计时作为X-Ray控制台的一个子段,所以我使用了AWSXRay。CaptureAyncFunc函数,并为所有sql查询添加了它,并且我能够打印跟踪日志。以下屏幕截图显示了GET请求的段和子段。 我想在AWS X-Ray管理控制台中看到亚节。在

  • 方法fragmentTransaction.add(Fragment,String)不适用(实际参数int无法通过方法调用转换转换为Fragment) 我的代码的一部分:

  • 我的照片应用程序里有一个片段。当用户编辑照片时, null 我的片段异步任务: 我在调用getParentFragmentManager()时遇到异常。我提到了一个相关的帖子,Fragment MyFragment not attached to Activity,但它与Fragment not be Activity相关。所以我不确定添加是否可以解决我的问题。理想情况下,在尝试弹出backSta

  • HomeFragment.java fragment_home 我看了其他问题。但我的应用程序不起作用。所以我想知道我是否需要修理我的主activity。所以我附上了主activity。 mainactivity.java 对不起,我英语不好。所以在翻译的帮助下...我第一次问了stackoverflow。 我在为韩国人做一个应用程序。但我在碎片上有些问题。 如果输入了webview,App将被关