An intent is a bundle of instructions to the app framework that can be used to start activities and services, and to send and receive broadcast messages between different parts of an app. To start a second activity, you create an intent object that includes the class definition of the target activity. You then call the startActivity() method, and the application framework takes care of the rest.
- [Instructor] Most Android apps have more than one activity, that is, more than one screen. And so it's important to know from the beginning how to create new activities, how to register them in your application manifest, and then how to navigate to them when the user asks for them. I'll demonstrate this in the project named Intents. An intent is a package of instructions. It's a way of telling the framework what you want to do, and one of the most common uses of intents is to tell the framework to navigate to a different activity.
I'm going to start this demonstration by creating a new activity. I'll go to the app module directory in the project window. Then I'll right-click and choose New, Activity, and I'll see a listing of common activities I might want to use. Many applications have a settings activity, a place where the user can express their preferences for how the application behaves. And this particular template will build a basic settings activity that shows you a lot of what you can do with settings. I'll choose that, and then I'll make one change in this dialog.
Here, where it asks me what the hierarchical parent is, I'll click the Browse button, and choose Main Activity. This means that main activity will be the parent of the settings activity. And when the user indicates that they want to return from the settings activity, the main activity is where they'll end up. I'll click Finish, and a whole bunch of things are going to happen. First of all, I'll have a new settingsactivity.java file. And this is a Java class that extends a class named appcompactpreferenceactivity.
And it has a whole bunch of Java code that's going to be managing the appearance of the activity and some of its values. There is also a class named appcompactpreferenceactivity. That's the super class and it extends preference activity. Now, you don't need to worry about this super class too much, so I'm just going to close that down. And then I'm going to go to the manifest, and show you that the settings activity has been added to the manifest automatically. Just like the main activity, it starts with a dot prefix, and that's shorthand for your base package.
That is to say, settings activity is in com.example.androidFirstapp, just like everything else in the application so far. It has an attribute named parentactivityname, that points to the parent activity. But if I move the cursor over the warning, I'll see the indication that that attribute is only used in more recent versions of Android. In an older version, which in this case means Android 4.0, I need to use this metadata tag to do the same thing. It uses a name attribute and a value attribute to point to the parent activity.
Even though this warning is here, you should keep both settings in place as long as you're supporting API level 15. Now, let's see how we're going to get to the activity. Right now, if I were to start up the application, I won't have any way to navigate to it. I have an options menu that appears up here, and it has an item named settings, but when I click on that nothing happens. Here's how that options menu is being created. As the activity starts up, a method named onCreateOptionsMenu is called automatically.
This is an override. That is, I'm replacing the existing version of the method from the super class. My version of onCreateOptionsMenu overrides the version from the super class. When this method is called, there's a bit of code that uses something called an xml inflater class. The inflater class reads the xml file and turns it into something that you can use that's Java based. And it's using a resource called menumain.xml. This is stored in the menu sub-directory under resources.
And this is where you can define your options menu. Right now, my options menu only has a single item and it has an ID of action settings. Notice that used the same ID prefix in an xml menu file as you do in a layout file. It's a way of uniquely identifying each item in the menu. So I'll come back to the main activity class. Now we know how the options menu is being created. There's also a method here called onOptionsItemSelected.
It's also an override, and it's being called automatically from the framework any time the user chooses an item in the menu. There's a bit of conditional code down here that compares the ID of the selected item to the particular ID that I'm interested in, action_settings. And I can control or command click to once again navigate back to that menu and to that specific menu item. I'm going to add a bit of code right here after the if statement.
In order to navigate to the activity, I'm going to use an intent object. An intent in Android is a package of instructions to the framework. In this example, it'll include an indication of which class I want to open as an activity. There are many other uses of intent in Android though. You can use an intent to send instructions to services and other components. I'll start by declaring an object that's typed as intent, and this is from the Android.content package from the core SDK.
I'll name it intent, and I'll instantiate it using the no arguments constructor of the class. There are a number of different ways of creating an intent object. I'm going to use the fifth version that starts off with a context and then asks for a Java class definition. The activity is one kind of context. The other kind is called the application context. So in order to pass in a context object, I just need to pass in this, meaning the current activity, the current context.
Then I'll tell the intent which activity I want to open. I'll pass in settingsactivity.class. Now notice I'm not passing in an instance of the activity class. I'm passing in the class definition. And it's up to the application framework to instantiate the activity and bring it to the screen. Next, I'll start the activity by calling a method named startactivity. And notice again that there are a number of different versions of this. There's two versions of startactivity, two of something called startactivityfromchild, startactivityfromfragment and so on.
I'm using the simplest version of this. I'll call startactivity, and I'll pass in the intent object. And that's all I need to do. Now when the user chooses the item from the options menu, the activity should start. So I'll run the application again. I'll go to my options menu and choose settings, and there's my settings activity. And this particular activity has a number of sub-activities, and the goal here is to show you all of the different kinds of preferences that you might want to use in your application.
If I go back to the main settings screen, and then click the back icon at the top, also known as the up button, I'll come back to the main activity, and again that's because I declared this activity as the parent. So those are the basics of working with multiple activities. You create your activity, and any associated layout or menu files. You then register the activity in the application manifest, and then to navigate to the activity you use an intent object. A lot of this work can be done for you by Android Studio, but they're all critical tasks when you want to use multiple activities in your Android application.
- Installing Android Studio
- Creating your first Android Studio project
- Managing profile files, including Gradle scripts and support libraries
- Defining screens with activities
- Implementing designs in XML layouts