Android Intent Parameter passing

Source: Internet
Author: User

Android Intent parameters pass the Android parameter pass in two ways,1is to pass parameters from one activity to the second activity and get the data in the second activity2is to pass parameters to the first activity after the second activity is closed (1) The first type: Public classFirstdemoactivityextendsActivity {PrivateButton Button3; PrivateEditText EditText; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.first); Button3= (Button) This. Findviewbyid (R.id.button3); EditText= (EditText) This. Findviewbyid (R.ID.EDITTEXT1); Button3.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {//TODO auto-generated Method StubString content=Edittext.gettext (). toString (); Intent Intent=NewIntent (firstdemoactivity. This, Seconddemoactivity.class); //The parameters to be passed can be passed in a bundle, and bundles can be seen as a special map.Bundle bundle=NewBundle (); Bundle.putstring ("Result", "content of the first activity"); Bundle.putstring ("Content", content);                Intent.putextras (bundle); //can also be passed in this way. //Intent.putextra ("result", "content of the first activity");startactivity (Intent);    }        }); }} receive Parameter Method Public classSeconddemoactivityextendsActivity {PrivateTextView TextView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.second); TextView= (TextView) This. Findviewbyid (R.ID.TEXTVIEW3); Intent Intent=getintent (); String result=intent.getstringextra ("Result"); String content=intent.getstringextra ("Content"); Textview.settext (Result+" : "+content); }}(2The second way to do this is to specify Requestcode when jumping to the second activity, and also to specify ResultCode before the second activity is closed, so that we can accurately determine if we need the data when we get the return value. 1. Jump to a second activity and specify ResultCodePrivateOnclicklistener btnAdd_Click =NewOnclicklistener () { Public voidOnClick (View arg0) {Intent Intent=NewIntent (); Intent.setclass (Getapplicationcontext (), lifingaddactivity.class); //startactivity (intent);        /** If you want to start another activity and want to have a return value, you need to use the Startactivityforresult method, * Instead of the StartActivity method * The first parameter is in Tent object, the second parameter is a Requestcode value, and if there are multiple buttons to start activity *, requestcode marks the activity that each button launches*/Startactivityforresult (Intent,1000); //Set Toggle animation, enter from right, exit on leftoverridependingtransition (R.anim.in_from_right, r.anim.out_from_left); }};2. In the first activity override Onactivityresult method/*** Events triggered after the activity is closed*/@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    //TODO auto-generated Method Stub    Super. Onactivityresult (Requestcode, ResultCode, data); //based on Requestcode to determine whether another activity opened for the current activity, according to ResultCode to determine what the specific operation of the second activity closed    if(Requestcode = = && ResultCode = = 1) {        NewThread () { Public voidrun () {String result=intent.getstringextra ("Result");    }}.start (); }}3. Specify resultcode and data before the second activity is closed/*** Save Button Event*/PrivateOnclicklistener Btnsave_click =NewOnclicklistener () {@Override Public voidOnClick (View v) {if(Checkinput ()) {NewThread () { Public voidrun () {Bundle data=NewBundle (); Data.putstring ("Result", "success"); Intent Intent=getintent ();                    Intent.putextras (data); Setresult (1, intent); Finish (); //Finish Current Activityoverridependingtransition (R.anim.in_from_left, r.anim.out_from_right);        }}.start (); }    }};

Android Intent Parameter passing

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.