Intents allow your current activity to launch another activity. They are a versatile mechanism that Android uses to connect different components. An Intent can be used to launch an activity that you have created, but it can also launch activities from other apps. Intents are also used for communication between two activities.
We will use Intents in many contexts during the course. Today, let's learn how to launch the new activity we created in the previous section.
In your first activity, create a button and the onClick function so that we can write some code. Refer to previous lessons if you are having trouble with this.
In the onClick function, write this line:
Intent intent = new Intent(this, SecondActivity.class);
There are some new terms here, but let's first focus on what is recognizable about this. The equal sign tells us that we are performing an assignment here. The left side is a variable declaration; the type is Intent, and the name of the variable is intent. The right side looks like we are somehow making a new Intent associated with SecondActivity and assigning it to the variable.
Even without learning more, we can get a general idea of what is happening here. When you encounter unfamiliar bits of code, look for areas of familiarity like this. Right now, let's learn a bit more. The new keyword allows us to create a new Intent. You might notice that we are using Intent as if it was a function, adding brackets and giving it input parameters. This is a constructor, which allows us to create new Intents. We will learn more about constructors when we learn about classes. The first input to this constructor is this, which refers to the current activity (MainActivity). The second input is the activity we want to go to.
We've created the Intent, so let's use it. Write this line next:
startActivity(intent);
As the name suggests, startActivity launches a new activity using the intent we have given as an input.
If you run the app and click the button, the second activity should be launched. This might be hard to tell if both layouts are blank, so go ahead and make them different (perhaps by adding a TextView to each, identifying which activity's layout this is).
Thoughts:
- Why did we need two lines of code to launch an activity? It might see useless to first create an intent and then use it to launch an activity. As we learn more about intents, however, we will see that we can use intents to do many more things, such as transfer information to the new activity.
- Can we see the starting of the new activity in the log? Yes! Remember the Activity Lifecycle we talked about. Those functions allow you to monitor the phases that an activity goes through. In the onCreate() of the new activity, add a print statement to write to the console. In the first activity, you can even see some of the other lifecycle states. For example, inside of the MainActivity, start typing onPause. As you type, the autocomplete should kick in, and you can hit tab to let it create the function for you. You can add log statements inside so it looks like this:
@Override
protected void onPause() {
super.onPause();
Log.d("MainActivity", "paused");
}
Now, when you click the button and the second activity launches, you can see the first activity being paused. Try this with onStop() and onResume() as well to see the sequence of events as you go back and forth the two activities. For your reference, your code might look like:
@Override
protected void onResume() {
super.onResume();
Log.d("MainActivity", "resumed");
}
@Override
protected void onPause() {
super.onPause();
Log.d("MainActivity", "paused");
}
@Override
protected void onStop() {
super.onStop();
Log.d("MainActivity", "stopped");
}