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 new project, passdata.
2. Code in Main. xml.
[Java]View plaincopy
- <? 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. In the Res/layout folder, create the secondactivity. xml file.[Java]View plaincopy
- <? 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]View plaincopy
- 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 ){
- // --- 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. The code in androidmanifest. xml.[Java]View plaincopy
- <? 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. Manoel. 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. The code in passdataactivity.
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.manoel.PassingDataSecondActivity"); //---use putExtra() to add new key/value pairs--- i.putExtra("str1", "This is a string"); i.putExtra("age1", 25); //---use a Bundle object to add new key/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. debug by F11.
The program is started for the first time:
Jump to secondactivity:
Return passdataactivity: