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
Asked
Active
Viewed 117 times
1
-
Did you calling fragment by menu item Id? – Hussnain Haidar Feb 13 '19 at 12:40
2 Answers
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
-
I'll be damned but this works!!. Thanks. Let me mark it as the correct answer – Beast77 Feb 13 '19 at 13:01
-
-
It does work. I am using Java though to implement the AAC navigation component. The idea is the same though. Once again, thanks. Back to work now. – Beast77 Feb 13 '19 at 13:05
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
-
1If 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
-
2Guys, check out the answer I have marked as correct above. It is brilliant – Beast77 Feb 13 '19 at 13:02