Once you've set up a navigation drawer, handling its menu events looks almost exactly like working with other menus. You can set up the activity as a listener object and implement callback methods that evaluate and react to menu item selections.
- [Narrator] As I described previously,…you can handle events from a navigation view menu…by implementing a listener object.…In this example application, based on a template…that's included with Android Studio,…the main activity class implements…NavigationView.OnNavigationItemSelectedListener,…and then, in order to implement that,…there's a method down toward the bottom of the class…named onNavigationItemSelected.…Notice the override annotation.…That indicates that this is an implementation…of an abstract method.…
When the user selects an item, two things happen.…First, there's an evaluation of the menu item's ID,…just like with an options or a pop-up menu.…There's code here to get the ID from the item,…and then to match it against the various items…that are in the navigation menu,…and then critically, down at the bottom of the method,…there's a bit of code to close the drawer.…That doesn't happen automatically.…You have to make that happen…by calling the closeDrawer method.…Now, I'm going to implement just one item from the menu.…
David also shows how to send and receive broadcast messages that let you exchange data and notifications among the different tiers of your application. He demonstrates how to work with menus and the action bar, add a navigation drawer to a layout, and tackle advanced navigation techniques. Later courses in the Essentials series concentrate on other specific skills you can master.
- Working with events
- Handling events with Java objects and lambda expressions
- Managing navigation
- Sending data to an activity
- Opening other apps with implicit intents
- Sending and receiving broadcast messages
- Working with menus and the action bar
- Adding a navigation drawer
- Advanced navigation techniques
- Removing activities from the back stack