Intent Data Transmission

Source: Internet
Author: User

(1) The first is simple jump of the activity:

1 ),Activity switching is generally implemented through intent. Intent is the method for an activity to reach another activity. It describes the start point (current activity) and the end point (target activity ). A simple intent implementation is as follows)

Intent intent = new intent (); // create an intent object intent. setclass (activity1.this, activity2.class); // describes the start point and target startactivity (intent); // starts to jump // or directly uses intent = (intent) New intent (activity1.this, activity2.class)

2) In general, we want to switch the previousActivity1To the nextActivity2. In this case, we can use bundle to implement it. Bundle is equivalent to a data storage package used to store the data we want to convey. For example, intent is like an email with the sender address (original activity) and recipient address (target activity). Bundle is an attachment and can also be seen as the content of a letter. The following is a simple implementation:

A. Send with activity1:

Intent intent = new intent (); intent. setclass (activity1.this, activity2.class); // describes the starting point and target bundle = new bundle (); // creates a bundle object bundle. putstring ("something", "data sent from activity1"); // load data intent. putextras (bundle); // Insert the bundle into the intent, startactivity (intent); // start switching

  B. activity2 receives data from activity1:

Intent intent = This. getintent (); // get the existing intent object bundle = intent. getextras (); // get the bundle object in intent string = bundle. getstring ("something"); // gets the string in the bundle.

3) solved the problem of how to transmit data, then the next question is how to accept the processing results returned from the target activity. There are two methods. One is to accept the processing result in the onrestart method (onrestrart indicates the event triggered when the activity is restarted). The method implementation is the same as that of activit Y2 to accept data processing from activity1. Another method is to switch to the response mode. Here, the second method is used. The difference between the switch mode and the normal switch mode is that the normal switch mode does not return, but the reply mode does. Different Code implementations: 1. Switch from activity1 to activity2: startactivityforresult (intent, 0); 2. Return activity1: setresult (result_ OK, intent) from activity2 ). 3. Accept the returned result: protected void onactivityresult (). The Code is as follows;

A. Switch from activity1 to activity2:

Intent intent = new intent (); intent = intent. setclass (activityintent. this, anotheractivity. class); bundle = new bundle (); bundle. putstring ("string", et_string.gettext (). tostring (); intent. putextras (bundle); startactivityforresult (intent, 0); // different only here // activityintent. this. finish (); // The ondestroy () is triggered if finish is unavailable ();

    B. Return activity1 from activity2:

Intent intent = new intent (); intent = intent. setclass (anotheractivity. this, activityintent. class); bundle = new bundle (); bundle. putint ("result", "processing result of activity2"); intent. putextras (bundle); anotheractivity. this. setresult (result_ OK, intent); // result_ OK is the return status code anotheractivity. this. finish (); // ondestroy () is triggered ();

 

C. activity1 accept the returned result of activity2:

Protected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); Switch (resultcode) {// process the returned result case result_ OK: bundle = data according to the status code. getextras (); // get the bundle object in intent string result = bundle. getint ("result"); break; default: break ;}}

4. Return activity1 from activity2 (the finish method is called when activity1 is switched to activity2). You will find that all the data before activity1 is lost, simply put, data is not retained. Because of the finish method, ondestroy () is triggered so that activity1 is destroyed. The next time I came to activity1, it was a brand new activity1. The previous data is certainly not there. If you want to retain the status before the switchover, you can use either of the following methods: 1. When switching, do not call the finish () method. 2. Use sharedpreferences to save data. Sharedpreferences is a lightweight storage class used to save the status of some forms, such as text box values and button states, similar to session. Generally, data is stored in the onpause () method and extracted from onresume. The implementation is as follows:

A. Save data

// Pause: onstart ()-> onresume ()-> onpause () @ override protected void onpause () {super. onpause (); log. E ("lifecycle_activity1", "onpause ()"); // Save the data to sharedpreferences in a storage set similar to a session. editor savedata = getpreferences (0 ). edit (); savedata. putstring ("value", et_string.gettext (). tostring (); savedata. commit ();}

B. extract data

// Restart: onstart ()-> onresume () @ override protected void onresume () {super. onresume (); log. E ("lifecycle_activity1", "onresume ()"); // obtain the required data from the shared data storage object sharedpreferences getdata = getpreferences (0); string value = getdata. getstring ("value", null); If (value! = NULL) {et_string.settext (value );}}

 

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.