Skip to content

Using Android Intents Tutorial

The Activity You Know

While we already discussed opening a specific activity in a previous article we also need to look at how to interact with an activity we open. So previously, we used this line of code when opening our activity.

startActivity(new Intent(this, Hello1.class));

This is a call to the startActivity method which expects an Intent to be passed in as a parameter. This method opens a new Activity and then forgets about it completely. That fine if this is the behavior you want, but what if you want to know what the user did with the new activity? To accomplish this, we are going to use startActivityForResult. Let’s go ahead and implement our onListItemClick method to handle when the user clicks on a list item.

    protected void onListItemClick(ListView l, View v, int position, long id) {
    	if (position == 0) {
    		startActivityForResult(new Intent(this, GetName.class), 1701); 
    	if (position == 1) {
    		//Item 2
    	if (position == 2) {
    		//Item 3

The line of code we are interested in here is this one:

startActivityForResult(new Intent(this, GetName.class), 1701);

We are starting a new activity, but this time we are telling Android that we expect to get a result back. As we did with startActivity in this article we are passing in an Intent with the class of our Activity. We also pass in a request code. This can be any integer and can be used to determine which request is being examined. For now I’m just going to use 1701 because I’m a Sci-Fi fan. Don’t worry about this for now, just put whatever positive integer you want here (Android will only return codes >= 0).

So far, this isn’t any harder than just calling an activity. Okay, let’s go ahead and create the new class we are going to open like so:

package com.learnandroid.intents;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class GetName extends Activity implements OnClickListener {
	private EditText txtName;
    public void onCreate(Bundle savedInstanceState) {
        txtName = (EditText) findViewById(;
        Button btnSave = (Button) findViewById(;
	public void onClick(View v) {
		Intent data = new Intent();
		data.putExtra("Name", txtName.getText().toString());
		setResult(RESULT_OK, data);

The only thing that should be new to you here is the contents of our onClick method.

Intent data = new Intent();
data.putExtra("Name", txtName.getText().toString());
setResult(RESULT_OK, data);

First we create a new Intent to use for passing data back in our response. The intent provides “Extras” as a way of passing extra information we need. We put an extra in the intent, in our case whatever string the user has provided. Then we call setResult in order to tell Android what result the calling activity should be sent. Finally we call finish, which destroys our current activity and takes us back to the previous activity. One thing to note is that each extra is given a name. I just used “Name” as the name of my extra since that’s what it represents, but it can be any string. We will use the name to retrieve the extra data.

When the activity that was called using startActivityForResult finishes it sends its result back to the calling activity by calling the onActivityResult method. So let’s override that method and do something with our result.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if (resultCode == RESULT_OK) {
    		Toast helloToast = Toast.makeText(this, "You Entered: " + data.getStringExtra("Name") 
                    , Toast.LENGTH_LONG);
        	helloToast.setGravity(Gravity.CENTER, 0, 0);;

Just to keep things simple, I am just going to use a popup (covered in this article) to display the value that was in the EditText. The Intent variable named data is the same intent we placed into our result. I retrieve the name we put into an extra by calling getStringExtra here. Our final result should look like this.


Next: The Action You Know

{ 6 } Comments

  1. shub | May 30, 2012 at 5:07 pm | Permalink

    i am new to android can you please explain what is onclicklistner (not what is does) and why we use View.onclicklistner and what is view there

  2. krishna | August 10, 2012 at 2:19 am | Permalink

    very useful, thaks..

  3. dinesh | February 10, 2014 at 3:24 am | Permalink

    wow…amazing…you give simple and easy look to android layout…..

    thank you very much

  4. fixit | April 11, 2014 at 11:56 am | Permalink

    great post, very useful for beginers

  5. Alex Butler | September 27, 2014 at 3:59 am | Permalink

    Will you accept guest post content?

  6. Sheridan | September 27, 2014 at 7:28 am | Permalink

    Sure, I would be willing to accept a guest post. Basically, my criteria would be that I should be able to open Eclipse, go through your tutorial step by step, and have it work without needed to figure out any extra stuff.

    Basically, I started this website with a friend (he wrote the lots of lists post). I was very gung ho about it… and then I found myself going through a divorce. I’ve kicked around the idea of starting to do tutorials again, and would be glad to have new content on here.

Post a Comment

Your email is never published nor shared. Required fields are marked *