0

I am creating a simple project and I am using the navigation component. Here is what I am trying to achieve. I have a navigation graph and single activity that has FragmentContainerView that acts as NavHostFragment and NavigationView that displays the navigation drawer menu. From this activity, using the drawer menu item and navigation graph, I can navigate to a specific fragment. This fragment contains a bottom navigation view. How can I connect this bottom navigation to the activity's navigation to be able to press bottom navigation menu items and navigate to destinations in acitivity's navigation graph? I kind of did it with subgraphs: the destination had its own navigation graph, but this didn't work well, because after pressing the navigate back button, the user would navigate back to the start destination of this graph. enter image description here

For now the solution I found is that,but the back button navigation still goes back to the starting destination(welcomeFragment in my case) instead of going back to the previous destination(TeamMainFragment),which I assume means that the destination navigated from bottom navigation is not added to the back stack.

 @Override
public void onStart() {
    super.onStart();
    BottomNavigationView bottomNavigationView = binding.bottomNavigationView;
    NavigationUI.setupWithNavController(bottomNavigationView,Navigation.findNavController(binding.getRoot()));
}

Is there any better solution?

I also tried this

    NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
    NavController navController = navHostFragment.getNavController();
    BottomNavigationView bottomNavigationView = binding.bottomNavigationView;
    NavigationUI.setupWithNavController(bottomNavigationView,navController);

Still not working as wanted: when I am in insertTeamFragment and press the navigate back button enter image description here

it navigates back to welcomeFragment,instead of teamMainFragment.

Found a solution thanks to Adnan :

super.onStart();
        NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
        NavController navController = navHostFragment.getNavController();
        BottomNavigationView bottomNavigationView = binding.bottomNavigationView;
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            navController.navigate(item.getItemId());
            return true;
        });
This works as wanted:when in insertTeamFragment and press the back navigate button it navigates back to teamMainFragment.But still don't understand why the NavigationUI solution is not working.If anyone knows,please comment.
Barracuda
  • 477
  • 5
  • 19

1 Answers1

0

@Override public void onAttach(@NonNull Context context) { super.onAttach(context);

    OnBackPressedCallback callback = new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            //set it navigate(current frag to previous frag)
            setRetainInstance(true);
            requireActivity().finish();
        }
    };
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
Adnan haider
  • 553
  • 8
  • 21
  • I think this is not it. I already have drawer navigation in my activity.I want the bottom navigation to appear only when I have reached certain destination, that's why I added that bottom navigation to the destination and not the acitivity. – Barracuda Apr 08 '21 at 09:59
  • then why you dont use this. BottomNavigationView bottomNavigationView= findViewById(R.id.bottom); bottomNavigationView.setOnNavigationItemSelectedListener(item -> { switch (item.getItemId()){ } }); – Adnan haider Apr 08 '21 at 10:04
  • I am trying to make it work with NavigationUI.It does navigate,but when I navigate back,instead of going back to teamFragment destination(when I am in insertTeamFragment),it goes back to welcomeFragment,from where the drawer navigation navigated to teamMainFragment. – Barracuda Apr 08 '21 at 10:09
  • then set manually in your fragment i update my answer kindly look into it – Adnan haider Apr 08 '21 at 10:11
  • I did use the listener with navController.navigate(item.getItemId()); and it worked as I wanted(the back navigate button navigates to the correct destination), but i stil don't understand why it's not working with NavigationUI.NavigationUI.setupWithNavController – Barracuda Apr 08 '21 at 10:15
  • in navigation.xml dont attach dashboardfragment with menu it's a fault where you get this – Adnan haider Apr 08 '21 at 10:17
  • Sorry,can you explain because I don't understand. – Barracuda Apr 08 '21 at 10:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230884/discussion-between-adnan-haider-and-barracuda). – Adnan haider Apr 08 '21 at 10:19