Android Intent


An Intent with respect to android development is a mechanism for describing operations that need to be performed in an abstract manner.

As a class Intent belongs to android.content package.

package android.content;

It does implement Parcelable and Cloneable.

public class Intent implements Parcelable, Cloneable {}

Creating Intents

Intents can be created by instantiating the android.content.Intent class. Different Parameters can then be passed depending on the reason for the creation of the intent.

public Intent() {}
public Intent(Intent o) {}
public Intent(String action) {}
public Intent(String action, Uri uri) {}
public Intent(Context packageContext, Class<?> cls){}

Example

Intent i = new Intent(MainActivity.this, SecondActivity.class);

Intents have several methods commonly used.

Specifying Intent Actions

For example the getAction() method. This method gets used to get the general action to be performed. This action will then generally specify the way in which the information in the Intent should be interpreted.

public String getAction() {}

The actions can be set using the setAction() method.

public Intent setAction(String action){}

Limiting Intents to Packages

Intents can be limited to packages. We can do this explicitly using the setPackage(). We can then retrieve the package the intent is limited to using the getPackage().

public String getPackage() {}
public Intent setPackage(String packageName){}

Intent Categories

Categories do give out additional detaisl about the action the intent performs. During the resolution of an intent, only activities that provide all the requested categories get used.

Intent categories can be added, retrieved, removed or even checked.

To add an intent category you use the addCategory() method:

public Intent addCategory(String category) {}

We can retrieve a collection or set of categories:

public Set<String> getCategories() {}

We can also check if an intent has a given category:

 public boolean hasCategory(String category) {}

We can also remove intent category:

public void removeCategory(String category) {}

Putting Data To Intents

Let's now get to more common usage of intents. Intents get used alot to transfer data between activities.

We can pack data to intents, then start use them to start an activity and retrieve those data from the target activity.

1.First and foremost we instantiate the intent:

Intent i=new Intent(MainActivity.this, PageOne.class);

where the PageOne.class is the target activity. The first parameter in this case is a Context object while the second parameter target class.

2.Secondly we invoke the putExtra() methods of the Intent object. The intent class has a variations of these putExtra() methods with various parameters. These variations exist so as to enable us transfer various data types via intents. Some of the data types that can be transfered via intents include:

Simple Data Types

These are the simple data types that can be transfered via intent:

  1. Boolean : public Intent putExtra(String name, boolean value)
  2. Byte : public Intent putExtra(String name, byte value)
  3. Char : public Intent putExtra(String name, char value)
  4. Int : public Intent putExtra(String name, int value)
  5. Long : public Intent putExtra(String name, long value)
  6. Short : public Intent putExtra(String name, short value)
  7. Float : public Intent putExtra(String name, float value)
  8. Double : public Intent putExtra(String name, float value)
  9. Double : public Intent putExtra(String name, double value)
  10. CharSequence : public Intent putExtra(String name, CharSequence value)

Parceled Data types

A parceled/parcelable type is a class whose instance can be written to and read from a parcel. A Parcel is final class belonging to android.os package and is a container for data and object references that are sendable through an IBinder. T Here are built in methods for transferring such data:

  1. Parcelable : public Intent putExtra(String name, Parcelable value)|
  2. Parcelable[] :public Intent putExtra(String name, Parcelable[] value)

Transferring ArrayLists via Intents

These are teh built-in methods for transferring arraylists:

  1. ArrayList<Parcelable> : public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable>) value
  2. ArrayList<Integer> : public Intent putIntegerArrayListExtra(String name, ArrayList<Integer> value)
  3. ArrayList<String> : public Intent putStringArrayListExtra(String name, ArrayList<String> value)
  4. ArrayList<CharSequence> : public Intent putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)

Transferring Arrays via Intents

You can transfer the arrays of our simple data types via intents using the following methods:

column column
Boolean[] public Intent putExtra(String name, boolean[] value)
Byte[] public Intent putExtra(String name, byte[] value)
Char[] public Intent putExtra(String name, char[] value)
Int[] public Intent putExtra(String name, int[] value)
Long[] public Intent putExtra(String name, long[] value)
Float[] public Intent putExtra(String name, float[] value)
Double[] public Intent putExtra(String name, double[] value)
String[] public Intent putExtra(String name, String[] value)
CharSequence[] public Intent putExtra(String name, CharSequence[] value)

Transferring a Bundle via Intent

A Bundle is a data structure which maps strings to Parcelable values. They can also be packed via this method:

  1. Bundle : public Intent putExtra(String name, Bundle value

Transferring Serialized data via Intent

Serialization is the process of converting in memory objects into a rigid binary representation. This binary form is basically a sequence of bytes that can be passed around then be deserialized back into memory.

This is one of the techniques that can be used to pass around complex objects between activities.

Once we have the serialized class, we can pass it into the below method.

  1. Serializable : public Intent putExtra(String name, Serializable value)

EXAMPLES

Sending Data

Let's say we have two activities: MainActivity.java and PageOne.java. All these two are activities:

Activity Description
MainActivity.java Will contain a button that when we clicked we open the Second Activity(PageOne.java).We then transfer a simple string to that PageOne.java activity
PageOne.java Will contain a textview to display a string it will receive from MainActivity. But first it will need to unpack that data via the key specified in the MainActivity.

So say we do this in a button click:

openBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i=new Intent(MainActivity.this, PageOne.class);
                i.putExtra("MY_KEY","Hello World");
                MainActivity.this.startActivity(i);
            }
        });

So you can see from the above that:

Code Description
Intent i=new Intent(MainActivity.this, PageOne.class); Create an Intent Object with a constructor taking two parameters, a Context object and Class type.
i.putExtra("MY_KEY","Hello World"); Invoke the putExtra() method of the intent class, passing in two parameters: a String key and String value.
MainActivity.this.startActivity(i); Invoke the startActivity() method of the Activity class. We pass the Intent object as a parameter.

Well that will send data to SecondActivity.

Receiving Data

Now we need to receive data from MainActivity and show in a textview:

We can just do this inside the onCreate() method of that activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_one);

        TextView greetingTxt= (TextView) findViewById(R.id.myGreetingTxt);
        greetingTxt.setText(getIntent().getExtras().getString("MY_KEY"));
    }
  1. TextView greetingTxt= (TextView) findViewById(R.id.myGreetingTxt); : Just references us a TextView that we can use to display our greeting.
  2. greetingTxt.setText(getIntent().getExtras().getString("MY_KEY")); : First we retrieve an Intent object from this Activity by calling the getIntent() method. All activities have an Intent object that did start them. Once we have the Intent object we call it's getString() method passing in our key. This gives us a string data from the intent or null if it does exist. We set the data into our setText() method of the TextView.

Example - Send Data From MainActivity to Second Activity.

Say we have these two activities:

  1. MainActivity. java
  2. SecondActivity.java

And these two layouts for the layous above respectively:

  1. activity_main.xml
  2. activity_second.xml

Here's how we will send data to the second activity. These data we are receiving from an edittext named nameTxt and a spinner named launchYearSpinner for selecting a year:

    private void sendData()
    {
        Intent i=new Intent(this,SecondActivity.class);
        i.putExtra("NAME_KEY",nameTxt.getText().toString());
        i.putExtra("YEAR_KEY",launchYearSpinner.getSelectedItem().toString());
        startActivity(i);
    }

We pack data using the putExtra method. We start the second activity using the startActivity method of the activity class.

Then in the second activity we will receive our data. First we obtain the Intent using the getIntent() method.

Then we use the following methods from the Intent class:

  1. getStringExtra - we pass it a key. It allows us get a string from our intent.
  2. getIntExtra - we pass the key and a default value, in this case 0. This will give us an integer that we passed from the main activity.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        nameTxt= (TextView) findViewById(R.id.nameTxt2);
        yearTxt= (TextView) findViewById(R.id.propellantTxt2);

        Intent i=this.getIntent();
        String name=i.getStringExtra("NAME_KEY");
        int year=i.getIntExtra("YEAR_KEY",0);

        nameTxt.setText("NAME : "+name);
        yearTxt.setText("LAUNCH YEAR : "+String.valueOf(year));

    }

Full Code:

1. Layouts
(a). activity_main.xml

This layout will be inflated into the main activit's layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.tutorials.hp.dataactivity_activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
(a). content_main.xml

This layout will be included in the activity_main.xml.

We have an edittext where users will type the data they want to pass to the second activity.

We also have a spinner where the user will select the year. The year will be an integer.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.tutorials.hp.dataactivity_activity.MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FIRST ACTIVITY" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/nameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/nameEditTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:hint= "Name" />
    </android.support.design.widget.TextInputLayout>

           <Spinner
            android:id="@+id/sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />

    </LinearLayout>
</RelativeLayout>
(b). activity_second.xml

This activity will be inflated into the second activity's user interface.

We pass two TextViews.

These will show the data that we will receive from the main activity.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tutorials.hp.dataactivity_activity.SecondActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/nameTxt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NAME" />
        <TextView
            android:id="@+id/propellantTxt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PROPELLANT" />
    </LinearLayout>

</RelativeLayout>
2. Activities

(a). MainActivity.java

package com.tutorials.hp.dataactivity_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    private EditText nameTxt;
    private Spinner launchYearSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        nameTxt= (EditText) findViewById(R.id.nameEditTxt);
        launchYearSpinner= (Spinner) findViewById(R.id.sp);

        fillYears();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendData();
            }
        });
    }

    /*
    SEND DATA TO SECOND ACTIVITY
     */
    private void sendData()
    {
        Intent i=new Intent(this,SecondActivity.class);
        i.putExtra("NAME_KEY",nameTxt.getText().toString());
        i.putExtra("YEAR_KEY",launchYearSpinner.getSelectedItem().toString());
        startActivity(i);

    }

    private void fillYears()
    {
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1);
        adapter.add("2017");
        adapter.add("2018");
        adapter.add("2019");
        adapter.add("2020");
        adapter.add("2021");
        adapter.add("2022");

        launchYearSpinner.setAdapter(adapter);

    }

}

(b). SecondActivity.java

package com.tutorials.hp.dataactivity_activity;

import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    TextView nameTxt,yearTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        nameTxt= (TextView) findViewById(R.id.nameTxt2);
        yearTxt= (TextView) findViewById(R.id.propellantTxt2);

        Intent i=this.getIntent();
        String name=i.getStringExtra("NAME_KEY");
        int year=i.getIntExtra("YEAR_KEY",0);

        nameTxt.setText("NAME : "+name);
        yearTxt.setText("LAUNCH YEAR : "+String.valueOf(year));

    }

}

Quick Android Intent HowTos and Examples

1. How to Send an Email via Intent

Say you want users to send feedback via email.

First you need to instantiate the Intent with Intent.ACTION_SENDTO action passed as a parameter:

        Intent send = new Intent(Intent.ACTION_SENDTO);

Then may be get the text to be send from an EditText. Then build a Uri text, parse it and pass it over to the setData() method of the Intent class.

Then invoke the startActivity() assuming you are inside an activity. If you are in a Fragment then you need the activity reference or a Context object.

Here's a method to send us email.

void sendFeedback()
{
    String feedback;

    String subject = "Feedback";
    feedback=fftext.getText().toString();
    if(!feedback.trim().isEmpty())
    {

        Log.i("Send email", "");
        String TO = "[email protected]";

        Intent send = new Intent(Intent.ACTION_SENDTO);
        String uriText = "mailto:" + Uri.encode(TO) +
                "?subject=" + Uri.encode(subject) +
                "&body=" + Uri.encode(feedback);
        Uri uri = Uri.parse(uriText);

        send.setData(uri);
        startActivity(Intent.createChooser(send, "Send mail..."));
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Please Enter Text in Feedback Details", Toast.LENGTH_SHORT).show();
    }
}
2. One static method to reuse in sending emails

This method returns you an Intent object which then you can use to show dialog for sending email.

    public static Intent newEmailIntent(Context context, String address, String subject,
            String body, String cc) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
        intent.putExtra(Intent.EXTRA_TEXT, body);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_CC, cc);
        intent.setType("message/rfc822");
        return intent;
    }
3. How to Resolve Photo/Image from Intent

At the end of the day we want to return the image path.

public static String resolvePhotoFromIntent(Context context , Intent intent , String appPath) {
        if(context == null || intent == null || appPath == null) {
            LogUtil.e(LogUtil.getLogUtilsTag(HelpUtils.class), "resolvePhotoFromIntent fail, invalid argument");
            return null;
        }
        Uri uri = Uri.parse(intent.toURI());
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {

            String pathFromUri = null;
            if(cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                // if it is a picasa image on newer devices with OS 3.0 and up
                if(uri.toString().startsWith("content://com.google.android.gallery3d")) {
                    // Do this in a background thread, since we are fetching a large image from the web
                    pathFromUri =  saveBitmapToLocal(appPath, createChattingImageByUri(intent.getData()));
                } else {
                    // it is a regular local image file
                    pathFromUri =  cursor.getString(columnIndex);
                }
                cursor.close();
                LogUtil.d(TAG, "photo from resolver, path: " + pathFromUri);
                return pathFromUri;
            } else {

                if(intent.getData() != null) {
                    pathFromUri = intent.getData().getPath();
                    if(new File(pathFromUri).exists()) {
                        LogUtil.d(TAG, "photo from resolver, path: " + pathFromUri);
                        return pathFromUri;
                    }
                }

                // some devices (OS versions return an URI of com.android instead of com.google.android
                if((intent.getAction() != null) && (!(intent.getAction().equals("inline-data")))){
                    // use the com.google provider, not the com.android provider.
                    // Uri.parse(intent.getData().toString().replace("com.android.gallery3d","com.google.android.gallery3d"));
                    pathFromUri =  saveBitmapToLocal(appPath, (Bitmap)intent.getExtras().get("data"));
                    LogUtil.d(TAG, "photo from resolver, path: " + pathFromUri);
                    return pathFromUri;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(cursor != null) {
                cursor.close();
            }
        }

        LogUtil.e(TAG, "resolve photo from intent failed ");
        return null;
    }

Top Android Intent Examples

Let's look at some examples.

Lets continue with our android data passing series we had started earlier on.We had looked at how to pass data from activity to fragment and how to pass both a list/object from activity to activity as well as simple primitive data types.

Today we look at how to pass simple data types from the fragment to activity.We pass data via Intent object.  

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post