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.

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
then why you dont use this. BottomNavigationView bottomNavigationView= findViewById(R.id.bottom); bottomNavigationView.setOnNavigationItemSelectedListener(item -> { switch (item.getItemId()){ } });
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.
then set manually in your fragment i update my answer kindly look into it
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.