Android Study Notes (12)-several ways to transmit data with Intent

Source: Internet
Author: User

Several ways to transmit data with Intent


Click here to obtain the complete code


In addition to returning data from an activity, we often need to transmit data to the activity. For this purpose, we can use the Intent object to pass the data to the target activity.


1. Create a project named PassingData and add a Button in the activity_main.xml file:

    

2. Add a new secondactivity. xml file in the res/layout folder and add TextView and Button:

    
      

3. Create a new Class named SecondActivity in the current package and add the following code to SecondActivity. java:

Package com. example. 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 {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. secondactivity); // to obtain the data sent through the Intent object, use the getIntent () method to obtain the Intent object, // call the getStringExtra () of the object () to obtain the string value Toast set using putExtra. makeText (this, getIntent (). getStringExtra ("str1"), Toast. LENGTH_SHORT ). show (); // same as above. For integer values, call the getIntExtra () method (Note: if the name does not store a value, the default value is used, where 0) Toast. makeText (this, Integer. toString (getIntent (). getIntExtra ("age1", 0), Toast. LENGTH_SHORT ). show (); // to obtain the Bundle object, use the getExtras () method: For string values, use getString (); for integer values, use getInt () 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 v) {Intent intent = new Intent (); intent. putExtra ("age3", 45); // you can use the setData () method (described in the previous article) intent for callback. setData (Uri. parse ("Something passed back to main activity"); // set Intent and result code setResult (RESULT_ OK, intent); // close activity finish ();}}

4. Add the following code to the AndroidManifest. xml file:

                    
                                  
               
          

5. Add the following code to the MainActivity. java file:

Public void onClick (View view) {Intent intent = new Intent ("net. zenail. passingData. secondActivity "); // Method 1: two key/value pairs are added for the Intent object using the putExtra () method: String and integer intent. putExtra ("str1", "This is a string"); intent. putExtra ("age1", 25); // Method 2: Create a Bundle object and add two key/value pairs to it, use putExtras to add Bundle extras = new Bundle (); extras to the Intent object. putString ("str2", "This is another string"); extras. putInt ("age2", 35); intent. putExtras (extras); // start the activity and set 1 to the Request Code startActivityForResult (intent, 1) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper. onActivityResult (requestCode, resultCode, data); if (requestCode = 1) {if (resultCode = RESULT_ OK) {// use getIntExtra () to obtain putExtra () the integer Toast. makeText (this, Integer. toString (data. getIntExtra ("age3", 0), Toast. LENGTH_SHORT ). show (); // use getData () to obtain the string value Toast set with setData. makeText (this, data. getData (). toString (), Toast. LENGTH_SHORT ). show ();}}}

6. Running:

Click:


The following figure is displayed:





The message box disappears. click the button and the following screen is displayed: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140508/2014050808305313.jpg" alt = "\">



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.