Android Learning Intent Transfer data

Source: Internet
Author: User

The role of intent in activity is mainly two:

1. Start target activity

2. Transmitting data

Intent two situations when passing data: The next activity passes data and returns data from the following activity.

One, the next activity to pass the data is mainly to use intent as "messenger" to invoke,

The original activity needs to create a intent and use the Putextra (key, value) method to put the information that needs to be passed to the intent, and then start.

 Public void OnClick (view view) {      = "Hello,secondactivity";       New Intent (firstactivity.  this, secondactivity. class );      Intent.putextra ("Amsg", Meg);      StartActivity (intent);       }

Then in the target activity, we need to parse the intent, get the information and get it.

Intent Intent = Getintent (); // Get the intent object coming in. String data = Intent.getstringextra ("amsg"); // gets the key name of the key-value pair LOG.D ("secondactivity", data); // results can be displayed in Logcat

Process: Get the intent--to get the information--show

Second, return data to the previous activity

From the target activity to the original activity, the problem is that there may be a target and the original one-to-many situation, then the "agreed password" to carry out, otherwise the activity does not know the data back to whom.

Android provides a method for this startactivityforresult (Intent,requestcode), which requires the activity to be destroyed when the data is returned to the previous Activity,requestcode to determine the source of the data, It must be unique in order to be able to match exactly. Here is the code for the Click event under firstactivity:

@Override              Public void OnClick (view view) {                new Intent (firstactivity.  this, secondactivity. class );                Startactivityforresult (Intent,1);            }

In secondactivity, you need to provide data-related information and set your own "identification code" for pairing with the previous activity:

@Override              Public void OnClick (view view) {                new  Intent ();                Intent.putextra ("Datatrans", "data from the second activity display");                Setresult (result_ok,intent);                Finish ();            }

Note that the intent here is only used to return data, whereas the Setresult (result, intent) method is specifically designed for returning data to the previous activity, the result of which is the processing result, typically RESULT_OK or result_cancel two kinds, Intent, in turn, transmits the data back.

After the first step of the Startactivityforresult method to start secondactivity, when Secondactivity is destroyed, the Onactivityresult method in the firstactivity is called back, Therefore, the method needs to be rewritten in firstactivity:

@Override Public voidOnactivityresult (intRequestcode,intresultcode,intent data) {        Switch(requestcode) { Case1:                if(ResultCode = =RESULT_OK) {String data1= Data.getstringextra ("Datatrans"); LOG.D ("Firstactivity", data1); }                 Break;  Case2:                if(ResultCode = =result_canceled) {String data2= Data.getstringextra ("Second"); LOG.D ("FirstActivity2", data2); }                 Break;  Case3:                if(ResultCode = =RESULT_OK) {String data3= Data.getstringextra ("Third"); LOG.D ("Firstactivity", data3); }                 Break; default:        }    }

The above is the three cases of their own judgment, in fact, if only two activity, then only need a case to be able. It can be seen that in order to return the data successfully, it is necessary to match in case, then ResultCode also need to match successfully.

It can be understood that: a letter sent and received, need the address on the envelope and my address (requestcode) consistent, and sometimes the address is consistent, but the emphasis on the name (ResultCode) to be consistent, then the envelope will be sent to the recipient's hands.

After the secondactivity is destroyed by the finish () method, the Onactivityresult method is called, First select Requestcode (that is, the unique value in the firstactivity in Startactivityforresult), if present, according to the return status value in Setresult and Onactivityresult in the comparison, if the same, are displayed directly.

In the return data to the previous activity, have not yet thought how to come to the popular metaphor, next time to fill up.

Android Learning Intent Transfer data

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.