In addition to the ability to return data results from an Activity, it is also very common to transmit data to an Activity.
1. Create a project named PassData.
2. Code in main. xml.
[Java] <? 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: onClick = "onClick"
Android: text = "Click to go to Second Activity"/>
</LinearLayout>
<? 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: onClick = "onClick"
Android: text = "Click to go to Second Activity"/>
</LinearLayout> 3. Create the secondactivity. xml file in the res/layout folder.
[Java] <? 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">
<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: onClick = "onClick"
Android: text = "Click to return to main activity"/>
</LinearLayout>
<? 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">
<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: onClick = "onClick"
Android: text = "Click to return to main activity"/>
</LinearLayout> 4. Create an Activity subclass: SecondActivity. java.
[Java] package net. horsttnann. PassingData;
Import net. horsttnann. PassingData. R;
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 ();
}
}
Package net. horsttnann. PassingData;
Import net. horsttnann. PassingData. R;
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. Code in AndroidManifest. xml.
[Java] <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "net. horsttnann. PassingData"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "10"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". PassingDataActivity"
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: name = "net. horsttnann. PassingData. SecondActivity"
Android: label = "Second Activity">
<Intent-filter>
<Action android: name = "net. horsttnann. PassingDataSecondActivity"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "net. horsttnann. PassingData"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "10"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". PassingDataActivity"
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: name = "net. horsttnann. PassingData. SecondActivity"
Android: label = "Second Activity">
<Intent-filter>
<Action android: name = "net. horsttnann. PassingDataSecondActivity"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>
</Application>
</Manifest> 6. Code in PassDataActivity.
7. debug by F11.
:
The program is started for the first time:
Jump to SecondActivity:
Return PassDataActivity:
From the column of horsttnann