1. Using Eclipse, create a new Android project and name it PassingData.
2. Add the following statements in bold to the main.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:orientation=”vertical” >
<Button android:id=”@+id/btn_SecondActivity” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to go to Second Activity”android:onClick=”onClick”/>
</LinearLayout>
3. Add a new XML file to the res/layout folder and name it secondactivity.xml. Populate it as follows:
<?xml version=”1.0” encoding=”utf-8”?>
android:orientation=”vertical” >
<TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Welcome to Second Activity” />
<Button android:id=”@+id/btn_MainActivity” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to return to main activity” android:onClick=”onClick”/>
</LinearLayout>
4. Add a new Class file to the package and name it SecondActivity. Populate the SecondActivity
.java file as follows:
package net.learn2develop.PassingData;
import android.app.Activity; import android.content.Intent; import android.net.Uri;
import android.os.Bundle; import android.view.View; import android.widget.Toast;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity);
//---get the data passed in using getStringExtra()---
Toast.makeText(this,getIntent().getStringExtra(“str1”), Toast.LENGTH_SHORT).show();
//---get the data passed in using getIntExtra()---
Toast.makeText(this,Integer.toString( getIntent().getIntExtra(“age1”, 0)), Toast.LENGTH_SHORT).show();
//---get the Bundle object passed in---
Bundle bundle = getIntent().getExtras();
//---get the data using the getString()---
Toast.makeText(this, bundle.getString(“str2”), Toast.LENGTH_SHORT).show();
//---get the data using the getInt() method---
Toast.makeText(this,Integer.toString(bundle.getInt(“age2”)), Toast.LENGTH_SHORT).show();
}
public void onClick(View view) {
//---use an Intent object to return data---
Intent i = new Intent();
//---use the putExtra() method to return some
// value---
i.putExtra(“age3”, 45);
//---use the setData() method to return some value---
i.setData(Uri.parse(
“Something passed back to main activity”));
//---set the result with OK and the Intent object---
setResult(RESULT_OK, i);
//---destroy the current activity---
finish();
}
}
5. Add the following statements in bold to the AndroidManifest.xml file:
<?xml version=“1.0“ encoding=“utf-8“?>
package=”net.learn2develop.PassingData” android:versionCode=”1” android:versionName=”1.0” >
<uses-sdk android:minSdkVersion=”14” />
<application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” >
<activity android:label=”@string/app_name” android:name=”.PassingDataActivity” >
<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=”net.learn2develop.PassingDataSecondActivity” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
</application>
</manifest>
6. Add the following statements in bold to the PassingDataActivity.java file:
package net.learn2develop.PassingData;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast;
public class PassingDataActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
}
public void onClick(View view) { Intent i = new
Intent(“net.learn2develop.PassingDataSecondActivity”);
//---use putExtra() to add new name/value pairs--- i.putExtra(“str1”, “This is a string”); i.putExtra(“age1”, 25);
//---use a Bundle object to add new name/values
// pairs---
Bundle extras = new Bundle(); extras.putString(“str2”, “This is another string”); extras.putInt(“age2”, 35);
//---attach the Bundle object to the Intent object---
i.putExtras(extras);
//---start the activity to get a result back---
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode,
int resultCode, Intent data)
{
//---check if the request code is 1---
if (requestCode == 1) {
//---if the result is OK---
if (resultCode == RESULT_OK) {
//---get the result using getIntExtra()---
Toast.makeText(this, Integer.toString( data.getIntExtra(“age3”, 0)), Toast.LENGTH_SHORT).show();
//---get the result using getData()---
Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
7. Press F11 to debug the application on the Android emulator. Click the button on each activity and observe the values displayed.
How It Works
While this application is not visually exciting, it does illustrate some important ways to pass data between activities.
First, you can use the putExtra() method of an Intent object to add a name/value pair:
//---use putExtra() to add new name/value pairs--- i.putExtra(“str1”, “This is a string”); i.putExtra(“age1”, 25);
The preceding statements add two name/value pairs to the Intent object: one of type string and one of type integer.
Besides using the putExtra() method, you can also create a Bundle object and then attach it using the putExtras() method. Think of a Bundle object as a dictionary object — it contains a set of name/ value pairs. The following statementscreate a Bundle object and then add two name/value pairs to it. It is then attached to the Intent object:
//---use a Bundle object to add new name/values pairs---
Bundle extras = new Bundle(); extras.putString(“str2”, “This is another string”); extras.putInt(“age2”, 35);
//---attach the Bundle object to the Intent object---
i.putExtras(extras);
On the second activity, to obtain the data sent using the Intent object, you first obtain the Intent object using the getIntent() method. Then, call its getStringExtra() method to get the string value set using the putExtra() method:
//---get the data passed in using getStringExtra()---
Toast.makeText(this,getIntent().getStringExtra(“str1”), Toast.LENGTH_SHORT).show();
In this case, you have to call the appropriate method to extract the name/value pair based on the type of data set. For the integer value, use the getIntExtra() method (the second argument is the default value in case no value isstored in the specified name):
//---get the data passed in using getIntExtra()---
Toast.makeText(this,Integer.toString( getIntent().getIntExtra(“age1”, 0)), Toast.LENGTH_SHORT).show();
To retrieve the Bundle object, use the getExtras() method:
//---get the Bundle object passed in---
Bundle bundle = getIntent().getExtras();
To get the individual name/value pairs, use the appropriate method. For the string value, use the
getString() method:
//---get the data using the getString()---
Toast.makeText(this, bundle.getString(“str2”), Toast.LENGTH_SHORT).show();
Likewise, use the getInt() method to retrieve an integer value:
//---get the data using the getInt() method---
Toast.makeText(this,Integer.toString(bundle.getInt(“age2”)), Toast.LENGTH_SHORT).show();
Another way to pass data to an activity is to use the setData() method (as used in the previous section), like this:
//---use the setData() method to return some value---
i.setData(Uri.parse(
“Something passed back to main activity”));
Usually, you use the setData() method to set the data on which an Intent object is going
to operate (such as passing a URL to an Intent object so that it can invoke a web browser to view a web page; see the section “Calling Built-In Applications Using Intents” later in this chapter for more examples).
To retrieve the data set using the setData() method, use the getData() method (in this example data
is an Intent object):
//---get the result using getData()---