Application of Bundle object for data transmission between different Android activities

Source: Internet
Author: User

In an application, data may need to be transferred when you jump to another Activity. In this case, Bundle objects may be used;

In MainActivity, there is an Intent that directs to BActivity,

Intent

Copy codeThe Code is as follows :{

Intent intent = new Intent (Context context, Class <?> Class );
// New a Bundle object and import the data to be passed. The Bunde is equivalent to the Map <Key, Value> structure.
Bundle bundle = new Bundle ();
Bundle. putString ("name", "Livingstone ");
Bundle. putXXX (XXXKey, XXXValue );
// Add the Bundle object to Intent
Intent. putExtras (bundle );
// Call the Activity corresponding to intent
StartActivity (intent );

}

In BActivity, use the following code to obtain the data transmitted by MainActivity.

Bundle bundle = this. getIntent (). getExtras (); // obtain the passed Bundle that encapsulates the data.
String name = bundle. getString ("name"); // obtain the Value corresponding to name_Key
// What type of value is added for obtaining the value

--> Bundle. getXXX (XXXKey );

Return XXXValue

All of the above are general basic data types. When an object needs to be passed, you can implement Parcelable or Serializable interfaces for this object;

Use the Bundle. putParcelable (Key, Obj) and Bundle. putSerializable (Key, Obj) Methods to add the object to the Bundle, and then add the Bundle object to the Intent!

On the redirected target page, use Intent. getParcelableExtra (Key) to obtain the object that implements Parcelable;

On the redirected target page, use Intent. getSerializableExtra (Key) to obtain the object that implements Serializable;

During the study today, we found that Intent. putExtra (Key, Value); can also transmit data, including the objects mentioned above!

The implementation of the Serializable interface is very simple and will not be described;

The following describes how to implement the Parcelable interface:

Copy codeThe Code is as follows: public class Book implements Parcelable {
Private String bookName;
Private String author;

Public static final Parcelable. Creator CREATOR = new Creator () {// a CREATOR member variable must be defined here, or an error will be reported!

@ Override
Public Book createFromParcel (Parcel source) {// obtain data from Parcel. You need to obtain the object instance through this method when obtaining data.
Book book = new Book ();
Book. setAuthor (source. readString (); // read data from Parcel. Read data in the same order as write data!
Book. setBookName (source. readString ());
Return book;
}

@ Override
Public Book [] newArray (int size ){
Return new Book [size];
}
};

@ Override
Public int describeContents (){
Return 0;
}

@ Override // write Parcel
Public void writeToParcel (Parcel dest, int flags ){
Dest. writeString (author); // write data to Parcel, which is in the same order as reading data!
Dest. writeString (bookName );
}
}

For details about Parcel, refer to the following description:

A final class is used to write or read various data. All methods are writeValue (Object) and read (ClassLoader )! (Personal translation comprehension)

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.