Reputation: 793
I use a One-Activity-Multiple-Fragments approach and the JetPackNavigation with a NavGraph. I have added a BottomNavigationBar to navigate to 2 Fragments which works as it should. Now I want to put a backbuttom into the BottomNavigationBar with the intensions that if someone presses it, it should go back to the very last fragment visited. I want to know if something like this is possible and if I have to set the connections in the NavGraph from all Fragments to the other Fragments such that it can navigate back? The problem is that my NavGraph is quite large and contains many fragments.
Here you see a screenshot from the NavGraph where I indicated the 2 Fragments for the Navigation in the BottomNavigationBar (which work quite well).
Further, here you see the XML code for the BottomNavigationBar.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/Back_BottomNavigation"
android:icon = "@drawable/ic_baseline_arrow_left_24"
android:title = "Back" />
<item
android:id="@+id/FR_LanguageSelection"
android:icon = "@drawable/ic_add_circle_full"
android:title = "@string/Language" />
<item
android:id="@+id/Fragment1"
android:icon = "@drawable/ic_add_circle_full"
android:title = "Fragment1" />
</menu>
Do you know if I can implement the back-buttom such that it will go back to the last displayed Fragment? I'd appreciate every comment.
Does nobody have an idea how I can do this? I read quite often that the Jetpack Navigation is good for handling the backstack. Is this true and how can I do this?
Update: I inserted, as adviced in one answer, a Listener for the BottomNavigationView into the MainActivity (which hosts all the Fragments using the NavHostFragment). Now the back navigation works as it should. However, the normal Jetpack navigation using the other Bottoms of the BottonNavigationBar does not work anymore. When I click on the other bottoms, nothing happens (before adding the Listener in the Main Activity it was working perfectly). Do you have any idea, how I can solve this problem? Here is the code of the newly added Listener in the onCreateMethod of the MainActivity class:
//These commands were there before
final NavController navController = Navigation.findNavController(this,
R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation, navController);
//These are the new commands
binding.bottomNavigation.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.Back_BottomNavigation:
navController.navigateUp();
}
return true;
}
});
Any ideas about how to tackle this problem?
Upvotes: 0
Views: 184
Reputation: 680
set the behaivor you want in BottomNavigationView's OnNavigationItemSelectedListener
yourBottomNavigationView.setOnNavigationItemReselectedListener(object : OnNavigationItemSelectedListener{
@override fun onNavigationItemSelected (MenuItem item){
when(item){
//insert your condition to match your requirement
}
}
})
Upvotes: 1