I have a simple toy app that works on one mainActivity. It has a bottom navigation bar with separate nested graphs. For instance, the navigation graph "notification" has an additional destination to settings. If I move to "notification" >>> "settings" >>>> to "home"/"dashboard" nested navigation and get back to "notification", it will show the most recent state of nested graph, which is "settings".
How to make the bottom navigation to get to the startDestination, which is "notification", after I double click "notification" menu item? For instance, the first time I click "notification" it retrieves "settings", if I click the same "notification" item in the bottom navigation, it will get me to the startDestination "notification".
activity_main.xml
package com.example.myvagtest;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.myvagtest.databinding.ActivityMainBinding;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_nested3,R.id.navigation_nested2,R.id.navigation_nested1).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_nested1"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_nested2"
android:menuCategory="secondary"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_nested3"
android:menuCategory="secondary"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
</menu>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_nested1">
<navigation
android:id="@+id/navigation_nested1"
app:startDestination="@id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.example.myvagtest.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
</navigation>
<navigation
android:id="@+id/navigation_nested2"
app:startDestination="@id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.myvagtest.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
</navigation>
<navigation
android:id="@+id/navigation_nested3"
app:startDestination="@id/navigation_notifications"
>
<fragment
android:id="@+id/navigation_settings"
android:name="com.example.myvagtest.ui.SettingsFragment"
android:label="settings"
tools:layout="@layout/fragment_settings">
<action
android:id="@+id/action_navigation_settings_to_navigation_notifications"
app:destination="@id/navigation_notifications" />
</fragment>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.myvagtest.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications">
<action
android:id="@+id/action_navigation_notifications_to_navigation_settings"
app:destination="@id/navigation_settings" />
<action
android:id="@+id/action_navigation_notifications_pop"
app:popUpTo="@id/navigation_notifications"
app:popUpToInclusive="true" />
</fragment>
<action
android:id="@+id/action_navigation_nested3_self"
app:destination="@id/navigation_notifications"
app:launchSingleTop="true"
app:popUpTo="@id/navigation_notifications"
app:popUpToInclusive="true"/>
</navigation>
</navigation>
Update:
public void bottomNavigationReselection() {
navView.setOnItemReselectedListener(new NavigationBarView.OnItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.home_navigation) {
navController.navigate(R.id.action_global_home_navigation);
} else if (id == R.id.explore_navigation){
navController.navigate(R.id.action_global_navigation_explore);
} else if (id == R.id.library_navigation){
navController.navigate(R.id.action_global_navigation_library);
} else if (id == R.id.profile_navigation) {
navController.navigate(R.id.action_global_navigation_profile);
}
}
});
}
is enough to refresh main destinations.