Title:android.os.Parcelable document self-translation date:2014-12-05 09:16:06
Tags
Many of the known indirect subclasses of the Android.os.Parcelable interface are not listed here:
Class Overview
Interface for classes whose instances can is written to and restored from a
Parcel. Classes implementing the Parcelable interface must also have a static
Field called CREATOR, which is an object implementing the Parcelable.creator
Interface.
The interface is used for the classes that the instance can be written to and can be recovered from parcel (for the parcel to temporarily know that it is a container for storing data, I will write it in the Android IPC). The class implementing the Android.os.Parcelable interface must hold a static field named CREATOR that implements the Android.os.Parcelable interface.
Android.os.Parcelable interface Typical use, this is the official website example:
Package Me.androiddemo.canglangwenyue.androiddemo;
Import Android.os.Parcel;
Import android.os.Parcelable;
/**
* Created by Canglangwenyue on 12/5/14.
*/
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 ();
}
}
Summary (Introduction)
Nested Classes (nested Class)
1.interface parcelable.classloadercreator<t>
Specialization of Parcelable.creator that allows your to receive
The ClassLoader the object is being created in.
Parsing: The specialization of Parcelable.creator, which allows you to receive an object created internally by object ClassLoader.
2.interface parcelable.creator<t>
Interface that must is implemented and provided as a public CREATOR field that
Generates instances of your parcelable class from a Parcel.
Parsing: The interface must be implemented by the quilt class, and CREATOR is provided as a public field, CREATOR is used to instantiate your wrapper class from parcel.
Constants (constant)
1.int Contents_file_descriptor
Bit masks for use with describecontents (): Each bit represents a kind of object
Considered to has potential special significance when marshalled.
Parse: A bitmask for describecontents (), each of which represents the special meaning attached when it is grouped.
2.int Parcelable_write_return_value
Flag for use with Writetoparcel (Parcel, Int.): The object being written is a
return value, which is the result of a function such as "parcelable
SomeFunction () "," Void someFunction (out parcelable) ", or" void
SomeFunction (InOut parcelable) ".
Parse: Writetoparcel (Parcel, int) flag bit: As a return value, yes "parcelable
SomeFunction () "," Void someFunction (out parcelable) ", or" void
SomeFunction (inout parcelable) "Returns the result.
Public Methods (Common method)
1.abstract int describecontents ()
Describe the kinds of special objects contained in this parcelable ' s marshalled
Representation.
Parsing: Describes a variety of special objects that are contained in a grouped form of a wrapper object.
2.abstract void Writetoparcel (Parcel dest, int flags)
Flatten this object with to a Parcel.
Parse: Expands the object into parcel (the container in which the data resides).
The parcelable is suitable for passing custom objects through intent. Finally, an example of data transfer using Parcelable is given.
1. Send object activity, the content is very simple, click Button,intent carry object to jump to MainActivity2 (to receive object activity).
Package Me.androiddemo.canglangwenyue.androiddemo;
Import android.content.Intent;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.Button;
Import Java.util.HashMap;
/**
* @author Canglangwenyue
* Activity used to send a message
*/
public class Mainactivity extends Actionbaractivity {
Private Button Sendbutton;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Sendbutton = (Button) Findviewbyid (R.id.send_button);
Sendbutton.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent ();
person person = new person ();
Person.name = "Wenyue";
Intent.putextra ("Wenyue", person);
Intent.setclass (Mainactivity.this,mainactivity2.class);
StartActivity (Intent);
}
});
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.menu_main, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
Noinspection simplifiableifstatement
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}
The 2.mainactivity2 is used to receive object from Mainactivity and prints the length and content of the person.name.
Package Me.androiddemo.canglangwenyue.androiddemo;
Import android.content.Intent;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
/**
- @author Canglangwenyue
- Intent of receiving data */
public class MainActivity2 extends Actionbaractivity {
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main_activity2);
Intent Intent = Getintent ();
- Person person = Intent.getparcelableextra ("Wenyue");
- LOG.E ("MainActivity2 received message ' s length----->", string.valueof (Person.name.length ()));
- LOG.E ("MainActivity2 received message content------>", person.name);
}
@Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action bar if it is Present. Getmenuinflater (). Inflate (R.menu.menu_main_activity2, menu); return true; }
@Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent activity in a ndroidmanifest.xml. int id = item.getitemid ();
Noinspection simplifiableifstatement
- if (id = = r.id.action_settings) {
- return true;
- }
- return super.onoptionsitemselected (item);
} }
The implementation of the 3.Parcelable, the specific details have been mentioned before, not much to say.
Package Me.androiddemo.canglangwenyue.androiddemo;
Import Android.os.Parcel;
Import android.os.Parcelable;
/**
* Created by Canglangwenyue on 12/5/14.
* parcelable is commonly used in intent for the delivery of custom objects
*/
public class Person implements Parcelable {
public String name;
@Override
public int describecontents () {
return 0;
}
@Override
public void Writetoparcel (Parcel dest, int flags) {
Dest.writestring (name);
}
/*
Classes that need to override the CREATOR implementation android.os.Parcelable interface must hold a static field named CREATOR that implements the Android.os.Parcelable interface
*/
public static final creator<person> Creator = new Parcelable.creator<person> () {
/*
Overriding the Creator method
*/
@Override
Public person Createfromparcel (Parcel source) {
person person = new person ();
Person.name = Source.readstring ();
return person;
}
@Override
Public person[] NewArray (int size) {
return new person[0];
}
};
}
Finally give the result of log printing in MainActivity2, seeing is real, haha:
Attached demo, hope to everyone with help: Cang Wolf asked the moon
Android Parcelable Full parsing