Janks
Janks

Reputation: 171

Android Navigation remove toolbar from first fragment

I'm trying to implement the new Jetpack Navigation framework from google, but I'm running into an issue. I want to use my first fragment as a login page and don't want to have a toolbar in it. How can I remove the toolbar from one of the fragments, but then add it for subsequent ones?

Edit: Tried looking into the AppBarConfiguration, but that only seems to affect whether or not the back arrow shows up

Upvotes: 5

Views: 1225

Answers (1)

Janks
Janks

Reputation: 171

Ended up figuring out how to do it. According to the android documentation you have to add an OnDestinationChangedListener to the nav controller and then you can do a switch on all the different destinations that you need to change the constant UI elements within.

navController = Navigation.findNavController(this, R.id.nav_host_fragment);
final Toolbar toolbar = findViewById(R.id.toolbar);

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
    @Override
    public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
        int id = destination.getId();
        switch (id) {
            case R.id.mainFragment:
                toolbar.setVisibility(View.GONE);
                break;
            default:
                toolbar.setVisibility(View.VISIBLE);
                break;
        }
    }
});

Upvotes: 9

Related Questions