Android uses intent to allow different activity to switch and pass parameters

Source: Internet
Author: User

Basic usage:

An explicit

Intent intent=new Intent (Activity A. This,activity class); StartActivity (Intent);

  

Passing parameters

Intent intent=new Intent (Activity A. This,activity class); Intent.putextra ("Parameter name", "value to be transmitted, can be of any type"); StartActivity ( Intent);

Get parameters

In the target activity

Intent intent=getintent (); String Data=intent.getstringextra ("parameter name");

Advanced:

In addition to the StartActivity method, you can also use the Startactivityforresult method, which feels similar to the observer pattern

The first parameter of the Startactivityforresult method is the intent instance, and the second parameter is the custom Requestcode code, which can then be manipulated when activity B returns (press the button or the back key).

Need to rewrite the Onactivityresult function in activity armor

In activity Armor

Intent intent=new Intent (Activity A. this,activity. Class); Startactivityforresult (intent,1);

Return to activity A by button in activity b

Button btnback= (button) Findviewbyid (r.id.btnback); Btnback.setonclicklistener (new Onclicklistener () {    @ Override public    void OnClick (View v) {          Intent intent=new Intent ();        Setresult (result_ok,intent);        Finish ();            }});

Return to activity A by returning the button back (on the machine) in activity b

Need to rewrite the onbackpressed function

@Overridepublic void onbackpressed () {    Intent intent=new Intent ();    Intent.putextra ("Parameter name", "value");    Setresult (result_ok,intent);    Finish ();}

  

Finally, rewrite the Onactivityresult function in activity armor.

@Overrideprotected void on activityresult (int requestcode,int resultcode,intent data) {        switch (requestcode) {        Case 1:            if (RESULTCODE=RESULT_OK) {                    String Returndata=data.getstringextra ("parameter name");}            }    }

  

Where Case 1 is the second parameter that is stored when the intent is established, the Requestcode

Finish

Android uses intent to allow different activity to switch and pass parameters

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.