1

So I have an issue with AAC navigation. 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. 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

Beast77
  • 193
  • 1
  • 17

2 Answers2

1

If you are calling fragment by menu item id then do it in your activity like below.It will solve your problem.

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.

Hussnain Haidar
  • 2,200
  • 19
  • 30
0

You should use a FragmentManager and add the fragment to the BackStack.

I recommend taking a look at this post: Go back to the previous fragment

P. Vera
  • 315
  • 4
  • 11
  • 1
    If he uses Navigation AAC then he shouldn't need to touch the FragmentManager directly – EpicPandaForce Feb 13 '19 at 12:15
  • How should I do it @EpicPandaForce? Because the way suggested by P. Vera is for the older method of simply using a navigation drawer and not aac navigation – Beast77 Feb 13 '19 at 12:18
  • 2
    Guys, check out the answer I have marked as correct above. It is brilliant – Beast77 Feb 13 '19 at 13:02