Bundle implements data transmission between activities

Source: Internet
Author: User

When an activity jumps to another activity through intent, if you want to transfer data at the same time, you can use bundle to put the data in the bundle object, and then add the bundle object to the intent, in this way, parameters are added in batches,

Intent intent =NewIntent ();
Intent. setclass (ex03_10.This, Ex03_10_1.Class);
/*A new bundle object and the data to be passed in*/
Bundle bundle =NewBundle ();
Bundle. putdouble ("height", height );
Bundle. putstring ("sex", sex );
/*Assign the bundle object assign to intent*/
Intent. putextras (bundle );
/*Call activity ex03_10_1*/
Startactivity (intent );

 

I personally think that the lecture on intelligence is clear.

The bundle class is used to carry data. Similar to map, it is used to store values in the form of key-Value Name-value pairs. Compared with map, it provides various common types of putxxx ()/getxxx () methods, such as: putstring ()/getstring () and putint ()/getint (), putxxx () the getxxx () method is used to get data from the bundle object. The bundle actually uses the hashmap <string, Object> type variable to store the value put by the putxxx () method.

When the getxxx () method of the bundle object is called, The method obtains data from the variable internally and converts the data type. The conversion type is determined by the xxx method, the getxxx () method returns the converted value.

In addition, we can also add a single parameter. This method is more convenient to use, and only a small amountCode.

 
Intent intent =NewIntent ();
Intent. putextra ("name", "xxxxxx ");

Intent provides the putextra () method after various common types of overload, such as: putextra (string name, string value), putextra (string name, long value), in putextra () the method internally determines whether a bundle object exists in the current intent object. If it does not exist, a new bundle object is created. The value passed in by calling the putextra () method will be stored in the bundle object.

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.