Android Development: Pass data to the next activity, return data to the previous activity

Source: Internet
Author: User

1. Transfer data to an activity down

Activity1

1Button button=(Button) Findviewbyid (r.id.button1);2Button.setonclicklistener (NewOnclicklistener () {3             4 @Override5              Public voidOnClick (View v) {6Intent intent=NewIntent ("1111111111111111111");7Intent.addcategory ("Android.intent.category.MY_CATEGORY");8Intent.putextra ("AAA", "This is a message");9Startactivityforresult (Intent, 0x11);Ten             } One});

Activity2

1         Intent intent=getintent (); 2         String Data=intent.getstringextra ("AAA"); 3         Toast.maketext (Activity2.  This, data, Toast.length_short). Show ();

2. Return data to the previous activity

Activity2, button implementation returns to Activity1

1Button jumpbutton=(Button) Findviewbyid (R.id.jumpbutton);2Jumpbutton.setonclicklistener (NewOnclicklistener () {3 @Override4              Public voidOnClick (View v) {5Intent intent=NewIntent ();6Intent.putextra ("xxx", "This is second activity data!");7 Setresult (result_ok,intent);8 finish ();9             }Ten});

Activity1

Rewrite Onactivityresult ()

1 @Override2     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {4         Super. Onactivityresult (Requestcode, ResultCode, data);5           if(requestcode==0x11) {6             if(resultcode==RESULT_OK) {7Returndata=data.getstringextra ("xxx");8             }9         }  Ten}

Android Development: Pass data to the next activity, return data to the previous activity

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.