Pass Data Using an Intent Object

Description

The following shows you the various ways in which you can pass data between activities.

Example

Set up the manifest file


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2s.app" >
// w  ww  . ja  v  a2 s . c  o  m
    <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>

Populate the SecondActivity.java file as follows:


package com.java2s.app;
//from   w w w . ja  v  a  2s  . co  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);


        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;
// ww w.j  ava  2  s  . c om
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();
        }
    }

}
Pass Data Using an Intent Object




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS