首页 > 代码库 > android Parcelable完全解析
android Parcelable完全解析
title: android.os.Parcelable文档自译 date: 2014-12-05 09:16:06
tags:
android.os.Parcelable接口的已知间接子类很多,这里就不列举了:
Class Overview
Interface for classes whose instances can be 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.
该Interface用于实例可以被写入,并且可以从Parcel中恢复的classes(对于Parcel暂时只要知道它是一个存 放数据的容器就好了,我会在android IPC中写到它)。实现android.os.Parcelable接口的类必须持有一个实 现了android.os.Parcelable接口的名为 CREATOR 的静态字段。
android.os.Parcelable接口典型使用方式,这是官网的例子:
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 = http://www.mamicode.com/in.readInt();
}
}
Summary(简介)
Nested Classes(嵌套类)
1.interface Parcelable.ClassLoaderCreator<T>
Specialization of Parcelable.Creator that allows you to receive
the ClassLoader the object is being created in.
解析:专业化的 Parcelable.Creator,允许你接收的Object内部创建的 ClassLoader 对象.
2.interface Parcelable.Creator<T>
Interface that must be implemented and provided as a public CREATOR field that
generates instances of your Parcelable class from a Parcel.
解析:该接口必须被子类实现,而且CREATOR 作为公有字段来提供,CREATOR 用于从 Parcel中实例化你的可包装类.
Constants(常量)
1.int CONTENTS_FILE_DESCRIPTOR
Bit masks for use with describeContents(): each bit represents a kind of object
considered to have potential special significance when marshalled.
解析:用于 describeContents() 的位掩码,每一位代表它编组时附加的特殊含义。
2.int PARCELABLE_WRITE_RETURN_VALUE
Flag for use with writeToParcel(Parcel, int): the object being written is a
return value, that is the result of a function such as "Parcelable
someFunction()", "void someFunction(out Parcelable)", or "void
someFunction(inout Parcelable)".
解析:writeToParcel(Parcel, int)的标志位:作为一个返回值,是"Parcelable
someFunction()", "void someFunction(out Parcelable)", or "void
someFunction(inout Parcelable)"返回的result。
Public Methods(公共method)
1.abstract int describeContents()
Describe the kinds of special objects contained in this Parcelable‘s marshalled
representation.
解析:描述各种特殊对象,它们包含在可包装对象的编组形式中.
2.abstract void writeToParcel(Parcel dest, int flags)
Flatten this object in to a Parcel.
解析:将该对象展开到Parcel(存放数据的容器)中。
Parcelable适用于通过Intent来传递自定义对象。最后给出一个用Parcelable进行数据传送的例子
1.发送Object的Activity,内容很简单,点击Button,Intent携带Object跳转到MainActivity2(用来接收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
*/
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 will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2.MainActivity2用来接收来自MainActivity的Object,并打印Person.name的长度和content。
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 */
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 will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
//noinspection SimplifiableIfStatement- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
} }
3.Parcelable的实现,具体细节前面已经讲到了,就不多说了。
package me.androiddemo.canglangwenyue.androiddemo;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by canglangwenyue on 12/5/14.
* Parcelable常用于Intent中进行自定义对象的传递
*/
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);
}
/*
需要重写Creator实现android.os.Parcelable接口的类必须持有一个实现了android.os.Parcelable接口的名为 CREATOR 的静态字段
*/
public static final Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
/*
重写Creator方法
*/
@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];
}
};
}
最后给出在MainActivity2中log打印的结果,眼见为实,哈哈:
再附上demo下载地址,希望对大家与帮助: 苍狼问月
android Parcelable完全解析