How to send and receive data (two methods) in Android?

Source: Internet
Author: User

1, if the button in the Mainactivity.java click on the settings:

Send data method Simple Type
I.putextra ("txt", "Yes, I just came from the message!");
Send data Method 2--Complex Type
Bundle data = new bundle ();
Data.putstring ("txt", "amount, I am a complex type of data method sent to!") ");
I.putextras (data);

When the return value is included, start with the Startactivityforresult (intent, Requestcode) method, without startactivity (intent), i.e.: Startactivityforresult (i, 0) ;

2. At the same time, set the receive processing return value method:

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {

The return value will be processed here

Super.onactivityresult (Requestcode, ResultCode, data);
}

3. Set the result data on the return value page Page1.java

Intent i = new Intent ();
I.putextra ("Rerult", "I am the return value Wow! ");
Setresult (0, I);

4. The return value is processed in the Receive return value page;

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {

Determines whether a value is non-null
if (data! = NULL) {
String result = Data.getstringextra ("Rerult");
Textview1.settext (result);
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

Tips:textview1 is the text field within the associated layout file that receives the return value page.

5, OK, details can download the following source code: source Download

How to send and receive data (two methods) in Android?

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.