Android How to - Return Results from an Intent








The startActivity() method invokes another activity but does not return a result to the current activity.

To pass data back from an activity, you should instead use the startActivityForResult() method.

Example

Set up the manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2s.app" >
//  w w w.j  a  v a  2s .com
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.java2s.app.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="Second Activity"
            android:name=".SecondActivity" >
            <intent-filter >
                <action android:name="com.java2s.SecondActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Add the following statements in bold to SecondActivity.java

package com.java2s.app;
/*from   w w  w.  jav  a  2s.  c  o m*/
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout parentContainer = new LinearLayout(this);
        parentContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        parentContainer.setOrientation(LinearLayout.VERTICAL);

        Button button = new Button(this);
        button.setText("Second");

        Toast.makeText(this, getIntent().getStringExtra("str1"), Toast.LENGTH_SHORT).show();

        Toast.makeText (this,Integer.toString(
                        getIntent().getIntExtra("age1", 0)),
                Toast.LENGTH_SHORT).show();

        Bundle bundle = getIntent().getExtras();

        Toast.makeText (this, bundle.getString("str2"),
                Toast.LENGTH_SHORT).show();

        Toast.makeText (this,Integer.toString(bundle.getInt("age2")),
                Toast.LENGTH_SHORT).show();
    }

    public void onClick(View view) {
        Intent i = new Intent();
        i.putExtra("age3", 45);

        i.setData(Uri.parse("Something passed back to main activity"));

        setResult(RESULT_OK, i);

        finish();
    }

}

Add the following statements to the MainActivity.java file

package com.java2s.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
//from  ww w  . j a v  a 2  s . c  o m
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout parentContainer = new LinearLayout(this);
        parentContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        parentContainer.setOrientation(LinearLayout.VERTICAL);

        Button button = new Button(this);
        button.setText("Open");
        parentContainer.addView(button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent("com.java2s.SecondActivity");
                i.putExtra("str1", "This is a string");
                i.putExtra("age1", 25);

                Bundle extras = new Bundle();
                extras.putString("str2", "This is another string");
                extras.putInt("age2", 35);

                i.putExtras(extras);

                startActivityForResult(i, 1);

            }
        });

        setContentView(parentContainer);
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Toast.makeText (this, Integer.toString(
                            data.getIntExtra("age3", 0)),
                    Toast.LENGTH_SHORT).show();
            Toast.makeText (this, data.getData().toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

}
null




Note

To call an activity and wait for a result to be returned from it, you need to use the startActivityForResult() method, like this:

startActivityForResult(new Intent("com.java2s.SecondActivity"),request_Code);

If the request code is set to -1, then calling it using the startActivityForResult() method is equivalent to calling it using the startActivity() method. No result will be returned.