0

I have bottom navigation with navigation drawer setup with Navigation controller. I'm currently using the latest v2.4.0-alpha05 which manages backstacks for each bottom navigation tab separately. The problem is, I have a custom navigation drawer (linearlayout with bunch of textviews) and it is owned by activity itself and not the fragments. There is a profile option inside this drawer and I want to navigate to account fragment when I click it. Well I can do that with navController.navigate(R.id.action_global_accountFragment) but this fragment will now be added on top of backstack of home fragment. Whenever I switch back to my home fragment(through bottom navigation), I want backstack of home fragment only and not account fragment, i.e I want it either removed from stack or any way to switch to backstack to account fragment's whenever I click on profile view. Is there any way I can do either of these things or any other solution? I'm attaching my mainactivity oncreate and navcontroller's onDestinationChanged methods as well. Any help is appreciated.

 NavHostFragment navHostFragment =
            (NavHostFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.nav_host_fragment_activity_main);
    navController = navHostFragment.getNavController();

    setSupportActionBar(toolbar);


    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.homeFragment, R.id.listingsFragment, R.id.featuredFragment, R.id.categoriesFragment
            , R.id.accountFragment)
            .setOpenableLayout(drawerLayout)
            .build();


    Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);



     NavigationUI.setupWithNavController(toolbar,navController,appBarConfiguration);
     NavigationUI.setupWithNavController(navViewBottom,navController);


    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
            if (destination.getId() == R.id.homeFragment){
                    controller.popBackStack(R.id.homeFragment,false, true);    //popping these so that if account fragment is in stack, it will get popped as well, but that also pops all other fragments
                Objects.requireNonNull(getSupportActionBar()).show();
               Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
            }

            if (destination.getId() == R.id.categoriesFragment){
                
                controller.popBackStack(R.id.categoriesFragment,false);
                Objects.requireNonNull(getSupportActionBar()).show();
                Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
            }

            if (destination.getId() == R.id.listingsFragment){
                controller.popBackStack(R.id.listingsFragment,false);
                Objects.requireNonNull(getSupportActionBar()).show();
                Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
            }

            if (destination.getId() == R.id.featuredFragment) {
                controller.popBackStack(R.id.featuredFragment,false);
                Objects.requireNonNull(getSupportActionBar()).show();
                Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
            }
            if (destination.getId() == R.id.accountFragment){
                controller.popBackStack(R.id.accountFragment,false);
                Objects.requireNonNull(getSupportActionBar()).hide();
            }
            
        }
    });

This is the onclick of layout from which I want to navigate to account fragment

    @Override
public void onClick(View view) {
    
    if (view.getId() == R.id.relativeLayout_profile_navDrawer) {
        drawerLayout.closeDrawer(GravityCompat.START);
        navController.navigate(R.id.action_global_account);

    }

}

1 Answers1

0

I just set bottomNavView.setSelectedItemId(R.id.yourwantedfargment) and it did the job for me!