In our last Android Tutorial, we discussed the Android Hello World Application. In this Android tutorial we’ll delve into a basic building block of Android called Intent.
What is Android Intent?
Android intent is a data structure containing a description of a to-be-performed operation.
One of the most powerful features of Intent is that you can send asynchronously messages to other activities and services.
An intent is always handled by an Android component, namely an activity, a service or a broadcast receiver. In this tutorial, we will focus on activities as intent handlers.
Android Intent Types
- Explicit intent specify the component to start by name (the fully-qualified class name). You’ll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. When you create an explicit intent to start an activity or service, the system immediately starts the app component specified in the Intent object.
- Implicit intent do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.
How do we use android intent to launch activities?
To create an intent and launch an activity with it, we can write the following code within our caller activity:
Intent intent = new Intent(CallerActivity.this, CallingActivity.class);
startActivity(intent);
Running this code snippet has the following consequences:
- A new instance of CallingActivity is created
- The instance is pushed on top of the current task’s stack, which is the one the caller activity is in.
- The activity is started and brought to the foreground.
Using Android Intent Flags
Flags defined in the Intent class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity and how to treat it after it’s launched.
The described behaviour can be modified by specifying intent flags on the intent instance before passing the intent to startActivity(Intent), for example:
intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This will bring an existing instance of the called activity type present in the current stack to the foreground.
Project Structure
This project consists of two activities named FirstActivity.java and SecondActivity.java. Leaving aside the regulars, one distinguishable directory in the resource folder is anim. It contains Animation resource files which we’ll see at length later in this tutorial.
In this tutorial we will bring buttons into use to show Intents between Activities.
A Button is a Push-button which can be pressed, or clicked, by the user to perform an action.
It is inherited from android.widget.TextView Class.
android.widget.Button class is used to display a normal button. To make a button react when user clicks it, a click event must be registered.For this a OnClickListener() method is added. Here we’ll invoke the intent object inside the method. Following snippet shows how its done:
FirstActivity.java
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}});
Saw that? That’s nothing. That’s just 5 minutes of coding.
Adding Animation
Now let’s add a transition effect when the Intent is performed.
To do this, add two animation resource files named activity_in.xml and activity_out.xml under res->anim. These contain the transition effects when an Intent occurs. Following screenshot shows activity_in and activity_out correspondingly.
activity_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%" android:toXDelta="0" android:duration="200" />
</set>
activity_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%" android:duration="200" />
</set>
As you can see,the deltaX values are complementary in both the pics. This is obvious since the new activity moves in and the previous activity has to move out.
Now just add the following snippet after startActivity(intent) :
overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
Build and run the project in android emulator, you will get something like below.
In the next article we’ll discuss at length on Spinners, Toasts and Passing Data through Bundles. You can download android intent project from below link.