Android - Intent Example


We know about basics of activity. Now suppose if we want to link two activities. We want user to enter some information in FistActivity and then we wish to transfer that information to SecondActivity. How can we do that?
The answer is - by using intents.

In android we have components like Activity, Broadcast Receiver and Services. We can start these components with the help of Intent. Also intent object helps to pass data between these components, so intents are also used as a message passing object.

We can create an intent and can load it with some key-value pairs.

Screenshot of the tutorial

We will create FirstActivity and will add a username.

As user clicks on submit button, he will be presented with a second screen. The data from first activity will get passed to second activity using intent.

SecondActivity

 

Implementing the first activity to create intent.

Steps Involved :

  1. Implement a interface called OnClickListener which will have a method named onClick() to detect the click events. Example : when user press submit button, the onClick() method will be called.
  2. Get the reference of all the views from xml file.
  3. Add click listener to submit button by calling setOnclickListener() method.
  4. Now as the user clicks on submit button, the onClick() method gets called and we receive the View which was clicked.
  5. Now get the data from the view and convert it into string.
  6. Now create an object of Intent. The constructor of Intent here takes two arguments. First argument is the reference of our FirstActivity and the second is the reference of SecondActivty (The activity which we wish to start when user clicks the submit button).
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    	intent.putExtra("username", msg);
    	startActivity(intent);
  7. Load data inside intent by adding key-value pairs with the help of putExtra() method.

FirstActivity.java

package com.example.activityproject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FirstActivity extends Activity implements OnClickListener {
	EditText username;
	Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        username = (EditText) findViewById(R.id.editText1);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(this);        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first, menu);
        return true;
    }


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String msg = username.getText().toString();
		Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
		intent.putExtra("username", msg);
		startActivity(intent);
		
	}
    
}

 

Layout of FirstActivity

  • Layout is very basic with a Textview, EditText and a button.
  • You can simply drag drop view items to create this layout.

activity_first.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FirstActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="56dp"
        android:text="Username"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_centerVertical="true"
        android:text="Submit" />

</RelativeLayout>


Implementing the second activity to receive intent

Steps involved :

  • Get the reference of intent by calling getIntent() method. This method helps to give the reference of that intent which has started the activity. Having the reference of intent, we can fetch important data from intent.
  • Get the value of key "username" from intent and store it in a string variable.
  • Now we want to display the received information. So take the reference of TextView and set its text to received username. In this way we can display the received information.
package com.example.activityproject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class SecondActivity extends Activity {
	TextView username;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		Intent intent = getIntent();
		String msg = intent.getStringExtra("username");
		username = (TextView) findViewById(R.id.textView1);
		username.setText(msg);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.second, menu);
		return true;
	}

}

Layout file for second activity.

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="72dp"
        android:layout_marginTop="90dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

So now we are familiar with intents and activity. Now basics of android are in our head. Make sure to learn listview in android after learning intents.