Lucas Toledo
Lucas Toledo

Reputation: 1

How to use NavController (with a NavHost in a fragment) in a fragment rather than in an activity?

I have a MainActivity and in it I have a navController with a mainFragment that serves as NavHost for 4 fragments (Feed, Notifications, Profile and Help). My problem is with the third of these fragments, in which I need it also has its own navController with 3 options (Activities, Achievements, and Edit Profile). I tried to create the same way I created in MainActivity, with a fragment of NavHost within the Profile fragment, but the android did not accept it in this way, and I could not find any examples searching on Google. Does anyone know how to solve it?

https://developer.android.com/reference/kotlin/androidx/navigation/NavController

It follows the way I implemented in MainActivity, this "bottomNavigationView" is the menu that was made to transit among the fragments:

        navController = Navigation.findNavController(this, R.id.main_fragment);

        navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
        navDestination = destination;

        switch (destination.getId()) {

            case R.id.feed:
                bottomNavigationView.setCurrentActiveItem(0);
                break;
            case R.id.alerts:
                bottomNavigationView.setCurrentActiveItem(1);
                break;
            case R.id.profile:
                bottomNavigationView.setCurrentActiveItem(2);
                break;
            case R.id.help:
                bottomNavigationView.setCurrentActiveItem(3);
                break;

        }

    });
    bottomNavigationView.setNavigationChangeListener((view, position) -> {

        switch (position) {
            case 0:
                if (navDestination.getId() != R.id.feed) {
                    titleHeaderMain.setText(R.string.publicacao);
                    titleHeaderMain.setGravity(View.TEXT_ALIGNMENT_CENTER);
                    navController.navigate(R.id.feed);
                }
                break;
            case 1:
                if (navDestination.getId() != R.id.alerts) {
                    titleHeaderMain.setText(R.string.notificacoes);
                    navController.navigate(R.id.alerts);
                }
                break;
            case 2:
                if (navDestination.getId() != R.id.profile) {
                    titleHeaderMain.setText(R.string.perfil);
                    navController.navigate(R.id.profile);
                }
                break;
            case 3:
                if (navDestination.getId() != R.id.help) {
                    titleHeaderMain.setText(R.string.ajudar);
                    navController.navigate(R.id.help);
                }
                break;
        }

    });

Upvotes: 0

Views: 1313

Answers (1)

hery
hery

Reputation: 49

You have to rethink your navigation path. I had the same problem but it didn't work. I had to rework on it.

I put bottom navigation only in activity and hide or show it depending on destination.

Upvotes: 0

Related Questions