简体   繁体   English

如何确保设置片段的后退按钮使用 aac 转到启动片段

[英]How to make sure that back button for settings fragment goes to the launching fragment with aac

So I have an issue with AAC navigation.所以我有 AAC 导航的问题。 I have multiple fragments in one activity.我在一项活动中有多个片段。 I inflate the menu options in MainActivity so that each fragment has access to the menu item which opens the settings.我扩充了 MainActivity 中的菜单选项,以便每个片段都可以访问打开设置的菜单项。 The problem is that each time I press the back button on a fragment, it does not go back to the fragment that launched it.问题是每次我按下片段上的后退按钮时,它都不会回到启动它的片段。 It goes back to the original fragment.它回到原始片段。 Is there a way to make sure this behavior is assured?有没有办法确保这种行为得到保证? Thanks谢谢

If you are calling fragment by menu item id then do it in your activity like below.It will solve your problem.如果您通过菜单项 id 调用片段,那么在您的活动中执行如下操作。它将解决您的问题。

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return NavigationUI.onNavDestinationSelected(item,navController) || super.onOptionsItemSelected(item)
}

And your menu item:还有你的菜单项:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto">
<item
        android:id="@+id/settingsFragment"
        android:title="Settings"
        android:menuCategory="secondary"
        app:showAsAction="never" />

MenuCategory = "secondary" will not pop out back stack to start destination. MenuCategory = "secondary" 不会弹出返回堆栈到开始目的地。

You should use a FragmentManager and add the fragment to the BackStack.您应该使用 FragmentManager 并将片段添加到 BackStack。

I recommend taking a look at this post: Go back to the previous fragment我建议看一下这篇文章: 回到上一个片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM