What is intent in Android

What are Android Intentions?

Intentions are a fundamental issue for Android developers. It is impossible to develop Android applications without getting in touch with intent. In this tutorial, I'm going to teach you the intentions from A to Z.

What are intentions?

During a soccer game, teammates pass the ball around the field with the aim of sending it into the opponent's goal. The ball is handed over to the defenders by the team's goalkeeper. Next up is the midfielders, and if everything goes as planned, one of the strikers will send it into the opponent's net. It is assumed that the opposing goalkeeper couldn't keep him away!

In Android, sending messages is enabled through the object. With the aid of intent, Android components can request functionality from other Android components. If you open up the Instagram app on your phone and take a photo with it, you have been exploiting an intention. Intentions also help with communication between parts of an app; Movement from one screen (activity) to another is made possible by intent.

Think of it this way: all the components (applications and screens) of the Android device are isolated. The only way they communicate with each other is through intent.

Start activities with intent

As mentioned earlier, you can use intents to launch various components: activities, services, and broadcast receivers.

To start an activity, use the method.

Here is a snippet of code that shows how to start another activity on purpose.

Intent numbersIntent = new Intent (MainActivity.this, NumbersActivity.class); startActivity (numbersIntent);

First we create a new object and pass the class. Then we start a new activity with that intention.

Types of intentions

Android supports two types of intent: explicit and implicit. If an application defines its target component with an intention, it is an explicit intention. If the application does not name a target component, it is implicit intent.

Example of explicit intent

The code snippet from Code above is an example of explicit intent. Look at it again.

Intent numbersIntent = new Intent (MainActivity.this, NumbersActivity.class); startActivity (numbersIntent);

Here, is the target component of ours. That means that is the defined component that is called by the Android system. It is important to note (as in the example above) that explicit intent is usually used in an application as it gives the developer the best control over which class is started.

Implicit Intent Example

Here is an implicit intent:

Intent = new intent (Intent.ACTION_VIEW, Uri.parse ("http://www.tutsplus.com")); startActivity (intent);

Having the above code in your codebase allows your application to intentionally launch a browser component for a specific URL. But how does the Android system recognize the components that can respond to a particular intent?

A component can be registered via aPre-filter for a specific action. Intention filters can be statically registered in for componentsAndroidManifest.xml. Here is an example where a component is registered as a web viewer:

Use intent in an app

Let's write some code to see how it works. In this section, you'll create a small app to try out both intentions. The app has a small form to enter a first name and a last name. If the Submit When you click the button, both entered values ​​are passed to another activity. There is also a button to launch a browser of your choice. The selected browser opens at http://code.tutsplus.com.

Open Android Studio and generate your. You can set the name of the package to com.tutsplus.code.android.droidintent.

Your starts with some imports and the class declaration:

Package com.tutsplus.code.android.droidintent; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; Import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity

Then you override the method of initializing the activity with any saved state and activity layout (we'll create this later).

@Override protected void onCreate (Bundle savedInstanceState) super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); // button handlers go here

Next you get references to each of the buttons defined in the layout and append a click listener.

final Button submitButton = (Button) findViewById (R.id.submit_button); submitButton.setOnClickListener (new View.OnClickListener () @Override public void onClick (View v) EditText firstName = (EditText) findViewById (R.id.first_name_text); EditText lastName = (EditText) findViewById (R.id.last_name); String firstNameString = firstName.getText (). ToString (); String lastNameString = lastName.getText (). ToString (); Intents submitIntent = new intent (MainActivity.this, ShowActivity.class); submitIntent.putExtra ("firstNameString", firstNameString) ; submitIntent.putExtra ("lastNameString", lastNameString); startActivity (submitIntent););

For the Submit Button, set to trigger an action when the button is clicked. When there is a click, we grab the first and last name from the view and send them to the next activity:. The target component is explicitly defined with intent, so this is an example of explicit intent.

final Button browserButton = (Button) findViewById (R.id.browser_button); browserButton.setOnClickListener (new View.OnClickListener () @Override public void onClick (view v) Intent Intent = New Intent (Intent.ACTION_VIEW, Uri.parse ("https://code.tutsplus.com")); startActivity (Intent ););

For the browser button that creates a new intent to launch any application that matches the filter: which should handle a web url. In other words, a web browser is started. If you have more than one browser application installed on your device, you will be asked to choose one to open the website. This is an example of implicit intent.


The layout for will be very simple in this tutorial.

Here you have two and two each first name and surname. There is also a button to transfer the names and another to start your browser.

Build the

To complete our app you will need to create an activity to handle the explicit intent defined above. Create a new activity with the name. In this activity, the result of entering the first name and last name is displayed. It should look like this:

Package com.tutsplus.code.android.droidintent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class ShowActivity extends AppCompatActivity @Override protected void onCreate (Bundle savedInstanceState) super.onCreate (savedInstanceState); setContentView (R.layout.activity_show); Bundle of Extras = getIntent (). GetExtras (); String inputFirstName = extras.getString ("firstNameString"); String inputLastName = extras.getString ("lastNameString"); TextView showFirstName = (TextView) findViewById (R.id.show_first_name); TextView showLastName = (TextView) findViewById (R.id.show_last_name); showFirstName.setText (inputFirstName); showLastName.setText (inputLastName);

In this activity, you begin by passing the strings from. You can use the function to get a reference to the intent that triggered the start of this activity. Then you can access the passed strings. Finally, after you get the TextView instances from the layout, you display the values ​​you got.

ShowActivity layout

The layout for this activity is simple:

Test the app

Now you can create your app and try it out on your Android device!

Transferring data with bundles

You can also use bundles when communicating intent data.

This class allows for the storage of complex data and supports data types such as strings, characters, boolean, integers, etc. Here is an example of how part of MainActivity.java would look if you used.

// create bundle object Bundle b = new Bundle (); // Save data in the bundle b.putString ("firstNameString", firstNameString); b.putString ("lastNameString", lastNameString); // create intent object Intent submitIntent = new Intent (MainActivity.this, ShowActivity.class); // intentionally save the bundle object submitIntent.putExtra (b); startActivity (submitIntent);


In this tutorial, we got a brief introduction to using intents to create activities in Android. We looked at the difference between explicit and implicit intentions and wrote a simple example for each type.

For more information on intent, see the Android documentation. Understanding how they work is very important. As you build more apps, you will come across many different types of intents and how they are used.

In the meantime, you can check out some of our other posts on Android app development!