parcelable interface of Android for serialization
1. Ways to implement serialization:
There are two options for implementing serialization in Android: One is to implement the Serializable interface (which is supported by the javase itself)
, one is to implement the Parcelable interface (Android-specific features, efficiency than the implementation of serializable interface efficient, usable
Intent data transfer, and can also be used for interprocess communication (IPC). The implementation of the serializable interface is very simple and sound
It's a little more complicated to realize the parcelable interface, but more efficient, it is recommended to improve performance with this method.
Note: There are two ways to pass objects intent in Android: one is bundle.putserializable (Key,object) and the other is
Bundle.putparcelable (Key,object). Of course, these objects have certain conditions, the former is to achieve the serializable
interface, while the latter is implemented with the Parcelable interface.
2. Why should serialization be implemented?
Permanently saves the object, saves the object's byte sequence to the local file, and passes the object in the network through the serialized object;
Passes an object between processes through serialization.
3. Principles for choosing a serialization method
1) When using memory, parcelable performance is higher than serializable, so we recommend using Parcelable.
2) serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.
3) Parcelable cannot use the case where the data is to be stored on disk because parcelable is not good Guaranteed
The persistence of data is changed in the outside world. Although serializable is inefficient, it is advisable to make
With Serializable.
4. Application Scenario:
You need to pass some data between multiple parts (activity or service) through intent, simple types such as numbers,
String) can be placed directly into the intent. Complex types must implement the Parcelable interface.
5.Parcelable Interface Definition
public interface Parcelable
{
Content Description interface, basic no tube
public int describecontents ();
Write interface functions, packaging
public void Writetoparcel (Parcel dest, int flags);
Read interface, the purpose is to construct an instance processing of the class that implements the parcelable from the parcel. Because
The implementation class is still unknown here, so the template must be used to inherit the class name passed through the template parameters.
In order to be able to implement the template parameters of the incoming, defined here creator embedded interface, contains two interface functions respectively
Returns an instance of a single and multiple inherited classes.
public interface creator<t>
{
Public T Createfromparcel (Parcel source);
Public t[] NewArray (int size);
}
}
6. Implementing parcelable Steps
1) Implements Parcelable
2) Rewrite the Writetoparcel method to serialize your object to a parcel object, that is: writes the data of the class to
Externally provided parcel, package the data that needs to be passed to the parcel container to save the data from the parcel container
3) Rewrite the Describecontents method, the Content interface description, the default return 0 can be
4) Instantiate the static internal object Creator Implement Interface Parcelable.creator
public static final Parcelable.creator<t> Creator
Note: The public static final one can not be less, the name of the internal object creator can not be changed,
All uppercase must be capitalized. The two methods in this interface need to be rewritten: Createfromparcel (Parcel in) implements a
The parcel container reads the passed data value, encapsulates it into a Parcelable object, returns the logical layer, NewArray (int size)
Create an array of type T, length size, and just one sentence (return new t[size]) for external
Class is used to deserialize the array of this class.
In short: Map your objects into parcel objects through writetoparcel, and then through Createfromparcel
Map the Parcel object to your object. You can also think of parcel as a stream, by writetoparcel the object
Writes to the stream, reads the object from the stream through the Createfromparcel, but this process requires you to implement,
So the order of writing and the order of reading must be the same.
The code is as follows:
public class Myparcelable implements Parcelable
{
private int mdata;
public int describecontents ()
{
return 0;
}
public void Writetoparcel (Parcel out, int flags)
{
Out.writeint (Mdata);
}
public static final parcelable.creator<myparcelable> Creator = new Parcelable.creator<myparcelable> ()
{
Public myparcelable Createfromparcel (Parcel in)
{
return new myparcelable (in);
}
Public myparcelable[] NewArray (int size)
{
return new Myparcelable[size];
}
};
Private myparcelable (Parcel in)
{
Mdata = In.readint ();
}
}
The difference between the 7.Serializable implementation and the Parcelabel implementation
1) Serializable implementation, only need implements Serializable can. It's just a call to the object
A tag and the system automatically serializes it.
2) The implementation of Parcelabel requires not only the implements Parcelabel, but also the addition of a
Static member variable Creator, this variable needs to implement Parcelable.creator interface.
Comparison of the two codes:
1) Create the person class to implement serializable
public class person implements Serializable
{
Private static final long serialversionuid = -7060210544600464481l;
private String name;
private int age;
public String getName ()
{
return Name
}
public void SetName (String name)
{
&NBS P THIS.name = name;
}
public int getage ()
{
return age;
}
public void Setage (int.)
{
THI S.age = age;
}
}
2) Create book class, implement Parcelable
public class book implements Parcelable
{
private String BookName;
Private String author;
private int publishdate;
Public book ()
{
}
Public String Getbookname ()
{
return bookname;
}
public void Setbookname (String bookname)
{
This.bookname = BookName;
}
Public String Getauthor ()
{
return author;
}
public void Setauthor (String author)
{
This.author = author;
}
public int getpublishdate ()
{
return publishdate;
}
public void setpublishdate (int publishdate)
{
This.publishdate = publishdate;
}
@Override
public int describecontents ()
{
return 0;
}
@Override
public void Writetoparcel (Parcel out, int flags)
{
Out.writestring (BookName);
out.writestring (author);
Out.writeint (publishdate);
}
public static final parcelable.creator<book> Creator = new creator< Book> ()
{
@Override
public book[] NewArr ay (int size)
{
return new book[size];
,
@Override
&nbs P Public book Createfromparcel (Parcel in)
{
&NBS P return new book (in);
}
};
Public book (Parcel in)
{
Booknam E = in.readstring ();
Author = in.readstring ();
Publishdate = In.readint ();
}
}
Parcelable interface of Android for serialization