I have a MainActivity and 3 different Fragments. The toolbar I created in MainActivity appears in all 3 Fragments I have. And I can switch between these Fragments using the button.
As an example;
binding.buttonSelectFile.setOnClickListener(v -> NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_ThirdFragment));
I want to create a similar behavior for the Toolbar item. For example, every time the user presses a "help" item defined as below, I want the application to navigate to the HelpFragment.
<menu
<!-- other items -->
<item
android:id="@+id/action_help"
android:orderInCategory="100"
android:title="@string/action_help"
app:showAsAction="never" />
</menu>
I tried to do something like this in the onCreate() method of the MainActivity class, purely as a guess.
binding.toolbar.getMenu().getItem(R.id.action_help).setOnMenuItemClickListener(item -> {
Navigation.findNavController(view).navigate(R.id.HelpFragment);
return true;
});
However, this method is of course not correct.
Is such use possible? Or should I follow another way to show the help screen to the user?