Josip Domazet
Josip Domazet

Reputation: 2850

Android NavigationComponent: Navigation Drawer not reacting

I am trying to use the new Navigation components from Android jetpack to create a navigation drawer. For some reason, the corresponding burger button does show up on the screen but doesn't react to clicks at all.

I've tried several tutorials online but to no avail. The last thing I tried was the offcial approach from https://developer.android.com/guide/navigation/navigation-ui.

MainActivity.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        setupNavigation();
    }

    private void setupNavigation() {
        drawer = findViewById(R.id.drawer_layout);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


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


        AppBarConfiguration appBarConfiguration =
                new AppBarConfiguration.Builder(navController.getGraph())
                        .setDrawerLayout(drawer)
                        .build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);


        NavigationView navView = findViewById(R.id.nav_view);
        NavigationUI.setupWithNavController(navView, navController);
   }

I don't understand why it isn't reacting to clicks at all.

Upvotes: 4

Views: 843

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199805

As per the ActionBar section of that same page:

Next, override onSupportNavigateUp() to handle Up navigation:

override fun onSupportNavigateUp(): Boolean {
    return NavigationUI.navigateUp(navController, appBarConfiguration)
        || super.onSupportNavigateUp()
}

Note that the AppBarConfiguration you create will need to be a variable at the class level as well so it can be used in both setupNavigation() and onSupportNavigateUp().

Upvotes: 4

Related Questions