I am using the navigation ui with single activity. In a particular fragment, I would like to perform an action when the arrow key on the toolbar is clicked. The toolbar currently works (takes user back to previous fragment), but I would want to perform an action before that is done (the action may even be to not go back to previous fragment).
Asked
Active
Viewed 222 times
0
-
Is the toolbar a common one (belongs to the activity and the fragments are replacing below it)? or each fragment has its own toolbar? is it a custom toolbar or a default one? – Geeky bean Nov 06 '21 at 21:20
-
Check [here](https://stackoverflow.com/questions/61777751/back-button-arrow-from-fragment-to-previous-home-fragment/61778000#61778000) – Zain Nov 06 '21 at 21:21
-
@Geekybean the toolbar is shared amongst all fragments. I have an activity which owns the toolbar, and fragments are simply inserted into a container layout in the activity – Ziyaad Shiraz Nov 07 '21 at 09:44
-
@Zain a click on the up arrow is not triggering any call to on optionsitemselected. Here is my code for the navigation uI: `AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(topLevelDestinations) .setOpenableLayout(drawerLayout) .build(); setSupportActionBar(toolbar); NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration); NavigationUI.setupWithNavController(navigationView, navController);` – Ziyaad Shiraz Nov 07 '21 at 09:55
-
@ZiyaadShiraz Did you add `setHasOptionsMenu(true)` in the fragment that has this up arrow? – Zain Nov 07 '21 at 10:11
-
@ZiyaadShiraz is the toolbar an 'androidx.appcompat.widget.Toolbar' object, or 'android.widget'? – Geeky bean Nov 07 '21 at 10:13
-
@Geekybean it is an androidx.appcompat.widget.Toolbar toolbar – Ziyaad Shiraz Nov 07 '21 at 11:10
-
@Zain sethasoptionsmenu seems to be a feature of a fragment, and i cannot thus add it to my main activity where onoptionsitemselected is being overriden – Ziyaad Shiraz Nov 07 '21 at 11:12
-
Yes, `setHasOptionsMenu` works for fragments, but for activities, you need to specify the parent activity in manifest with `android:parentActivityName` [Check this](https://stackoverflow.com/questions/19207762/must-i-specify-the-parent-activity-name-in-the-android-manifest) – Zain Nov 07 '21 at 14:05
-
@Zain yes i added it. Other menu items appear. – Ziyaad Shiraz Nov 13 '21 at 22:13
1 Answers
0
The solution turned out to be using setNavigationOnClickListener
on the toolbar instance, checking if the destination you are in matches the destination you wished to have that custom behavior, if yes, you perform the custom action there, if no, you navigate up normally. Here is the code snippet:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (navController.getCurrentDestination().getId() == R.id.addPersonalNoteFragment) {
System.out.println("do what ever you want to do on the back arrow press here");
} else {
NavigationUI.navigateUp(navController, appBarConfiguration);
}
}
});

Ziyaad Shiraz
- 144
- 2
- 7