Activity jump and value transfer for Android Learning

Source: Internet
Author: User

Activity jumps and transfers values mainly through the Intent class. Intent is used to activate components and accompanying data.


1. Activity jump

Method 1
Intent intent = new Intent (A. this, B. class );
StartActivity (intent)

Method 2
Intent intent = new Intent ();
Intent. setClass (A. this, B. class );
StartActivity (intent );

Implement jump from A to B (A and B are inherited from Activity)

Ii. Data Transmission

Activity A transmits data

Method 1
Intent intent = new Intent ();
Intent. setClass (A. this, B. class );
Intent. putExtra ("name", "xy ");
Intent. putExtra ("age", 22 );

StartActivity (intent );

Method 2
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


// Obtain parameter 1
Intent intent = this. getIntent ();
String name = intent. getStringExtra ("name ");
Int age = intent. getIntExtra ("age", 22); // The default value is 22.

// Obtain parameter 2
Bundle bundle = intent. getExtras ();
String name2 = bundle. getString ("name ");
Int age2 = bundle. getInt ("age", 22 );

Both methods can be used to obtain parameters. They are not exactly the same as the method for passing parameters 1 and 2.

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.