Android Learning activity jump and transfer value

Source: Internet
Author: User

Activity jumps and passes values. Mainly through the intent class. The role of intent is to activate the component and the accompanying data.


First, activity jumps

Method One
Intent Intent = new Intent (a.this, B.class);
StartActivity (Intent)

Method Two
Intent Intent = new Intent ();
Intent.setclass (A.this, B.class);
StartActivity (Intent);

Implementation from a jump to B (A, B are inherited from activity)

II. Transmission of data

Activity A transmits data

Method One
Intent Intent = new Intent ();
Intent.setclass (A.this, B.class);
Intent.putextra ("name", "XY");
Intent.putextra ("Age", 22);

StartActivity (Intent);

Method Two
Intent Intent = new Intent (a.this, B.class);
Bundle bundle = new bundle ();
Bundle.putstring ("name", "XY");
Bundle.putint ("Age", 22);

Intent.putextras (bundle);
StartActivity (Intent);


Activity B receives data


Get the number of references 1
Intent Intent = This.getintent ();
String name = Intent.getstringextra ("name");
int age = Intent.getintextra ("Age", 22); The default value is 22

Get the number of references 2
Bundle bundle = Intent.getextras ();
String name2 = bundle.getstring ("name");
int age2 = Bundle.getint ("Age", 22);

Both of these can be obtained, not with the corresponding method one by one.

Android Learning activity jump and transfer value

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.