Android Learning with 2 (intent implementation activity jump)

Source: Internet
Author: User

One, simple activities between the jump

BTN1 = (Button) Findviewbyid (R.ID.FIRST_BTN1);        Btn1.setonclicklistener (new  View.onclicklistener () {            @Override            public void OnClick (View v) {               new Intent (firstactivity.  this, Secondactivit. class );                StartActivity (intent);            }        });

explicit jump -jumps from firstactivity to secondactivity, and does not pass any other information.

Second, the implicit action mode jump

Add an action condition to the destination activity my.action,category setting to Default

...         <activity android:name= ". Secondactivit ">            <intent-filter>                <action android:name="my.action"/>                 < Category android:name= "Android.intent.category.DEFAULT"/>            </intent-filter>        </activity> ...

Set the action in the source activity that intent jumps to the desired response, which must match the action set in the destination activity.

        BTN1 = (Button) Findviewbyid (R.ID.FIRST_BTN1);        Btn1.setonclicklistener (new  View.onclicklistener () {            @Override            public void OnClick (View v) {                 new Intent ("my.action");                 StartActivity (intent);            }        });

Iii. transfer of data when jumping between activities

The interface functions provided by intent are putextra as key values, and the source activity code is as follows:

      BTN1 = (Button) Findviewbyid (R.ID.FIRST_BTN1);        Btn1.setonclicklistener (new  View.onclicklistener () {            @Override            public void OnClick (View v) {                 new Intent ("my.action");                Intent.putextra ("mytag", "myvalue");                StartActivity (intent);            }        });

At this time, the destination activity can get the data passed through the received intent, it is necessary to ensure that the key value is correct.

    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second);         = getintent ();         = Intent.getstringextra ("mytag");        Toast.maketext (this, Str,toast.length_short). Show ();    }

Android Learning with 2 (intent implementation activity jump)

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.