I have a side drawer made out of Navigation Component and when I navigate to a fragment with in a fragment and press back it pops the entire stack up to the root fragment. Let me explain.
I navigate from Root fragment to Fragment A (Both of these fragments are defined in the navigation XML file) and then from Fragment A to Fragment B
Root Fragment ---> Fragment A ---> Fragment B.
I have tried defining menuCategory:secondary to my navigation items , use action tag and defining "popupto" and "destination" parameters in the said tag but still no results.
Help would be appreciated.
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
if(prefsUtils.getFromPrefs(Keys.roleName).equals("PA") ||prefsUtils.getFromPrefs(Keys.roleName).equals("RSM"))
{
navController.setGraph(R.navigation.mobile_navigation);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_dashboard,R.id.nav_createOrder)
.setDrawerLayout(binding.drawerLayout)
.build();
}
else if (prefsUtils.getFromPrefs(Keys.roleName).equals("TP"))
{
navController.setGraph(R.navigation.mobile_navigation_tp);
binding.navView.getMenu().clear();
binding.navView.inflateMenu(R.menu.activity_main_drawer_tp);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_dashboardTP)
.setDrawerLayout(binding.drawerLayout)
.build();
}
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
@Override
public void onBackPressed() {
super.onBackPressed();
navController.navigateUp();
}
you can control where your present screen(Fragment B) can go by using
Here startActivity() function write fragment A so that it reverse backs to the fragment A when back button is clicked in Fragment B. Write all that above code in Fragment B java class.