Andorid Learning Path (vii) Serializable interface and Parcelable interface

Source: Internet
Author: User

Data transfer between the activity

Data transfer between activity we can transfer many different types of data through the Putextra method of the intent object, such as strings, integers, real numbers, objects, and so on.

First, take a string for example:

The transfer of strings via Putextra can be done in the following ways:

PutExtra (string name, String value): Intent

The first parameter is the string that is used to identify the activity that receives the data, which is equivalent to the index value in HashMap.

The second is the data to be sent (here is the string).

This value is found in the target activity through this name.

In the current activity:

String str = "Hello"; Intent.putextra ("Data", str);

In the target activity:

Bundle bundle = Getintent (). Getextras (); String str = bundle.getstring ("Data");

The above is just a simple explanation of how to transfer strings.

So how do you transfer objects? This is a special item and cannot be transferred like Putextra ("data", Object), without this method.

There are two more special methods that can be found in overloaded methods:

PutExtra (String name, Serializable value): Intent

PutExtra (String name, parcelable value): Intent

Serializable and parcelable are two interfaces respectively, so to transfer objects, you just need to make the class implement one of these two interfaces on the line.

Let's take a look at the serializable implementation, create a class obj and implement the interface:

Import Java.io.serializable;public class OBJ implements Serializable{private string Name;public string GetName () {return Name;} public void SetName (String name) {this.name = name;}}

Current Activity:

obj obj = new obj (); Obj.setname ("Li Gang"); Intent.putextra ("Data", obj);

Target activity:

Bundle bundle = Getintent (). Getextras (); obj obj = (obj) bundle.get ("Data"); String name = Obj.getname ();

Take a look at the parcelable implementation, create a class Obj2 and implement the interface:

Import Android.os.parcel;import Android.os.parcelable;public class Obj2 implements Parcelable{private String name; private string Age;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;} @Overridepublic int describecontents () {return 0;} /* * Create Creator constant (you must do this) * */public static parcelable.creator<obj2> Creator = new creator<obj2> () {@Overridep Ublic obj2[] NewArray (int size) {return null;} /* * Override the Createfromparcel method * */@Overridepublic Obj2 Createfromparcel (Parcel source) {//By reading the properties of the Parcel class object to set the properties of the current class Obj2 Obj2 = new Obj2 (); Obj2.setname (source.readstring ()); Obj2.setage (source.readstring ()); return obj2;}};/ * Save Object properties to the Parcel class object */@Overridepublic void Writetoparcel (Parcel dest, int flags) {dest.writestring (name); Dest.writestring (age);}}

Does it look more complicated than implementing the Serializable interface? But this method has to be much, mainly the method is fast, the performance is good (generally use this).

Current Activity:

Obj2 obj2 = new Obj2 (), Obj2.setname ("Li Gang"), Obj2.setage ("38 years old"), Intent.putextra ("S1", obj2);

Target activity:

Bundle bundle = Getintent (). Getextras (); Obj2 obj2 = (Obj2) bundle.get ("S1"); String name = Obj2.getname (); String age = Obj2.getage ();

These are two ways to implement object transfer.


The end~



Andorid Learning Path (vii) Serializable interface and Parcelable interface

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.