Android Program Development: (2) use intent-2.4 use intent to transmit data

Source: Internet
Author: User
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
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Button
  7. Android: Id = "@ + ID/btn_secondactivity"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: onclick = "onclick"
  11. Android: text = "click to go to second activity"/>
  12. </Linearlayout>

3. In the Res/layout folder, create the secondactivity. xml file.[Java]View plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Textview
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "Welcome to second activity"/>
  10. <Button
  11. Android: Id = "@ + ID/btn_mainactivity"
  12. Android: layout_width = "fill_parent"
  13. Android: layout_height = "wrap_content"
  14. Android: onclick = "onclick"
  15. Android: text = "click to return to main activity"/>
  16. </Linearlayout>

4. Create an activity subclass: secondactivity. java.[Java]View plaincopy

  1. Public class secondactivity extends activity {
  2. @ Override
  3. Public void oncreate (bundle savedinstancestate ){
  4. Super. oncreate (savedinstancestate );
  5. Setcontentview (R. layout. secondactivity );
  6. // --- Get the data passed in using getstringextra ()---
  7. Toast. maketext (this, getintent (). getstringextra ("str1 "),
  8. Toast. length_short). Show ();
  9. // --- Get the data passed in using getintextra ()---
  10. Toast. maketext (this,
  11. Integer. tostring (getintent (). getintextra ("age1", 0 )),
  12. Toast. length_short). Show ();
  13. // --- Get the bundle object passed in ---
  14. Bundle bundle = getintent (). getextras ();
  15. // --- Get the data using the getstring ()---
  16. Toast. maketext (this, bundle. getstring ("str2"), Toast. length_short)
  17. . Show ();
  18. // --- Get the data using the getint () method ---
  19. Toast. maketext (this, integer. tostring (bundle. getint ("age2 ")),
  20. Toast. length_short). Show ();
  21. }
  22. Public void onclick (view ){
  23. // --- Use an intent object to return data ---
  24. Intent I = new intent ();
  25. // --- Use the putextra () method to return some
  26. // Value ---
  27. I. putextra ("age3", 45 );
  28. // --- Use the setdata () method to return some value ---
  29. I. setdata (URI. parse ("something passed back to main activity "));
  30. // --- Set the result with OK and the intent object ---
  31. Setresult (result_ OK, I );
  32. // --- Destroy the current activity ---
  33. Finish ();
  34. }
  35. }

5. The code in androidmanifest. xml.[Java]View plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "net. horsttnann. passingdata"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Uses-SDK Android: minsdkversion = "10"/>
  7. <Application
  8. Android: icon = "@ drawable/ic_launcher"
  9. Android: Label = "@ string/app_name">
  10. <Activity
  11. Android: Name = ". passingdataactivity"
  12. Android: Label = "@ string/app_name">
  13. <Intent-filter>
  14. <Action Android: Name = "android. Intent. Action. Main"/>
  15. <Category Android: Name = "android. Intent. Category. launcher"/>
  16. </Intent-filter>
  17. </Activity>
  18. <Activity
  19. Android: Name = "net. Manoel. passingdata. secondactivity"
  20. Android: Label = "second activity">
  21. <Intent-filter>
  22. <Action Android: Name = "net. horsttnann. passingdatasecondactivity"/>
  23. <Category Android: Name = "android. Intent. Category. Default"/>
  24. </Intent-filter>
  25. </Activity>
  26. </Application>
  27. </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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.