Now that we have a basic understanding of handling events in Android, we can turn to navigation. Most Android apps have more than one screen, or activity. It’s up to the developer to implement functionality that lets the user navigate to other activities and back again.
- [Narrator] So far I've described the basics of handling events in Android. Now, I'll turn to navigation. Most Android Apps have more than one screen or activity. It's up to the developer to implement functionality that let's the user navigate to these other activities and get back again. To demonstrate basic navigation I'm working in a starting project called Start Activity. In this project there's a very simple main activity class that loads a layout file called Activity Main.
The Activity Main file has floating action button in the lower right corner. I'll look at it in design view to show you where that appears down here. And then it has a nested activity file called Content Main. And this is where this graphic and these three buttons are implemented. Labeled Starters, Entrees and Desserts. In this starting application none of these buttons do anything yet. The goal is to navigate to the appropriate activity when the user touches or clicks on one of those buttons.
And to demonstrate this I've added another activity to the application called Starters Activity. It's activity method loads a layout file called Activity Starters and that activity layout file displays some static text. A list of menu items that are in the Starters category. So let's go back to ContentMain.xml where those buttons are defined. And I'll go to text mode and scroll down to the Starters button down here. I'm going to add an on-click event handler and I'll create an event handler method called Display Starters.
I'll use and intention action and I'll create that event handler. Notice because I now have two activities in the application that I'm prompted to choose the one where I want to implement this method. I'll implement it in Main Activity and there's my event handler method. In order to navigate to the Starters Activity Class I'll create a new intent object. The intent class is a member of the package Android.content. I'll name the object intent, lower case, and I'll instantiate it with New Intent.
There are a number of constructor methods for the intent class and they're used for different purposes. When you want to navigate to the activity, use the one that starts with a context and then receives a class as its second argument. The context can be this, when you're creating an intent for navigation the context should be an activity, not the application context. Then pass in the class declaration of the activity you want to navigate to.
That'll be StartersActivity.class. Notice that I'm not passing in an instance of that class I'm passing in the class declaration. The application framework will handle the navigation for me. Now, I'm ready to launch the activity. I'll call a method of the activity class called Start Activity and I'll pass in the intent object. And that's all I need to do. And then I'll run the app on my virtual device. When the app opens I'll click on the Starters button and I navigate to the second activity.
Now if I want to get back to the first activity it's pretty easy, I can just touch the back button. The second activity is in the same task as the first activity. The first activity went to the back stack and so when I finished the new activity I returned to the first one automatically. But notice that when I go to that second activity that I don't have any sort of icon in the upper left corner that you might be used to seeing. That is called the Up icon and it's a button that's a part of the action bar.
In order to create it automatically you need to add a little bit of information in the application manifest. So I'll go back to Android studio and I'll open up my AndoidManifest.xml file. I'm going to close everything else because I'm only going to be making a change here. I'll go to the declaration of Starters Activity. This was added to my Manifest file when I created the activity class. Now I'm going to expand this to a beginning and ending tag and then within the activity class, I'll add something called a metadata element.
It looks like this, meta-data, and then I'll pass in a name attribute. The name will be a constant that looks like this. Android.support.PARENT_ACTIVITY. Be sure to spell it exactly this way and it is case sensitive. Then I'll pass in a value attribute. And this will point to the activity class that should be considered as the parent of this activity. And I'll choose .MainActivity. And now I'll run the application again.
This time when I click the Starters button I get the Up button on the top left corner. And again, that's part of what's known as the Action Bar or the Tool Bar. And when I click on the button I once again go back to the main activity but now instead of just walking up the back stack, I'm being very specific about which activity I'm returning to. This notation, the metadata element is for older versions of Android. And if you want to support both the most recent versions of Android and the older ones that's all you need to do.
If you're only supporting the most recent versions of Android you can instead use an attribute called parentACTIVITYName and set it to the same as the value attribute for the metadata element. Because my application is supporting both newer and older versions I'm only going to use the metadata element. And I get the behavior that I'm looking for. So in its simplest form, that's Android navigation. You use intents to describe where you want to go and then the application framework handles the details.
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