Functions of bundle classes in Android

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_9c153bc60101a0aq.html

Explanation: Bundle class is used to carry data. It is similar to map and 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. Bundle actually uses hashmap variables to store the values put by the putxxx () method:

Public final class bundle implements parcelable, cloneable {

......

Map MMAP;

Public bundle (){

MMAP = new hashmap ();

......

}

Public void putstring (string key, string value ){

MMAP. Put (Key, value );

}

Public String getstring (string key ){

Object o = MMAP. Get (key );

Return (string) O;

... // If the type conversion fails, null is returned. Here, the processing code after the type conversion fails is omitted.

}}

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.

Example:Use bundle to transmit data between activities

[1]. Transmit data from the source activity

// Write data to intent
Intent openwelcomeactivityintent = new intent ();
Bundle mybundelforname = new bundle ();
Mybundelforname. putstring ("key_name", inname. gettext (). tostring ());
Mybundelforname. putstring ("key_age", inage. gettext (). tostring ());
Openwelcomeactivityintent. putextras (mybundelforname );
Openwelcomeactivityintent. setclass (androidbundel. This, welcome. Class );
Startactivity (openwelcomeactivityintent );

 

Get data in the target Activity

// Retrieve data from intent
Bundle mybundelforgetname = This. getintent (). getextras ();
String name = mybundelforgetname. getstring ("key_name ");
Mytextview_showname.settext ("Welcome to:" + name );

 

Use bundle to transmit data between activities 2

 

Send a service request from the source request activity to the target activity through an intent.

Private intenttonextintent; // intent member declaration
Tonextintent = new intent (); // intent Definition
Tonextintent. setclass (twoactivityme3.this, secondactivity3.class );
// Set the next activity to be enabled
Startactivityforresult (tonextintent, request_ask );
// When intent is enabled, the request code is passed at the same time

 

Wait for intent to return the response result in the source request activity, and reload the onactivityresult () method.

@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
// Todo auto-generated method stub
Super. onactivityresult (requestcode, resultcode, data );
If (requestcode = request_ask ){
If (resultcode = result_canceled ){
Settitle ("cancel ****");
} Else if (resultcode = result_ OK ){
Showbundle = data. getextras (); // obtain the bundle from the returned intent
Name = showbundle. getstring ("myname"); // obtain the corresponding data from the bundle.
Text. settext ("the name get from the second layout: \ n" + name );
}
}
}

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.