Android Intent

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.
  • 1 What is Android Intent?
  • 2 Android Intent Types
  • 3 How do we use android intent to launch activities?
  • 4 Using Android Intent Flags
  • 5 Project Structure
  • 6 Working With Buttons
  • 7 Adding Animation

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 asynchronous messages to other activities and services.

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 asynchronous messages to other activities and services.
The 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 specifies 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 does 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 to the Intent object.

How do we use android intent to launch activities?

To create an intent and launch 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 on how to launch activity and how to treat it after it’s launched.
The described behavior 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);

Project Structure

Android-intent-project






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.

Working With Buttons

In this tutorial, we will bring buttons into use to show the Intent between Activities.A Button is a Push-button that 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 the user clicks it, a click event must be registered. For this, an OnClickListener() method is added. Here we’ll invoke the intent object inside the method. Following snippet shows how it's 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. The 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);
intent-calling
Build and run the project in android emulator, you will get something like below.




Post a Comment

0 Comments