0

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;
        }

    });
  • So what exactly are you having problems with? Can you point to what line you are having problems with and what the error message is? You can certainly nest a NavHostFragment within a Fragment as a child Fragment just like any other Fragment. – ianhanniballake Jun 17 '19 at 21:37

1 Answers1

0

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.

hery
  • 49
  • 6
  • 1
    Please elaborate. Why does OP have to rethink his navigation and how exactly did you fix the issue. Please provide an example. – m02ph3u5 Apr 25 '20 at 15:50