转载请注明出处,谢谢~
在SDK中 Parcelable类的概述是这样的: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 theParcelable.Creator
interface.
这个接口的实例是通过Parcel进行存储的,在使用Parcelable的时候必须使用Parcelable.Creator。
下面上代码:
首先修改main.xml,增加一个button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parcelable">
</Button>
</LinearLayout>
然后是主Activity ParcelableTest.java,这个类显示UI,并通过button点击事件启动另一个Activity -- ParcelableTest2,同时通过Parcelable接口传递一些数据。
package parcelable_test.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ParcelableTest extends Activity implements OnClickListener{
public static final String KEY = "key";
private Button button;
public static final String TAG = "Parcelable";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
Log.d(TAG, "ParcelableTest");
}
private void init(){
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
private void fun(){
Log.d(TAG, "fun");
Person mPerson = new Person();
mPerson.setName("tom");
mPerson.setAge(25);
Intent mIntent = new Intent(this,parcelable_test.com.ParcelableTest2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(KEY, mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
fun();
break;
default:
break;
}
}
}
ParcelableTest2.java,这个类用于获取ParcelableTest传出的数据,并显示在UI上。
package parcelable_test.com;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ParcelableTest2 extends Activity{
private static final String TAG = ParcelableTest.TAG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "ParcelableTest2");
TextView textView = new TextView(this);
Person mPerson = (Person)getIntent().getParcelableExtra(ParcelableTest.KEY);
textView.setText("name = " + mPerson.getName() + " age = " + mPerson.getAge());
setContentView(textView);
}
}
下面就是最重要的类Person,Person类引用了Parcelable接口
package parcelable_test.com;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Person implements Parcelable{
private String name;
private int age;
private static final String TAG = ParcelableTest.TAG;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
Log.d(TAG,"createFromParcel");
Person mPerson = new Person();
mPerson.name = source.readString();
mPerson.age = source.readInt();
return mPerson;
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
Log.d(TAG,"describeContents");
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
Log.d(TAG,"writeToParcel");
dest.writeString(name);
dest.writeInt(age);
}
}
最后在AndroidManifest.xml加入<activity android:name=".ParcelableTest2"></activity>
程序运行效果如下:
![](http://hi.csdn.net/attachment/201105/18/8630179_1305706042388L.png)
![](http://hi.csdn.net/attachment/201105/18/8630179_1305706063Imir.png)
输出的log如下:
05-18 16:09:47.243 I/ActivityManager( 59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=parcelable_test.com/.ParcelableTest }
05-18 16:09:47.383 D/Parcelable( 337): ParcelableTest
05-18 16:09:48.003 I/ActivityManager( 59): Displayed activity parcelable_test.com/.ParcelableTest: 724 ms (total 724 ms)
05-18 16:09:49.263 D/Parcelable( 337): fun
05-18 16:09:49.263 D/Parcelable( 337): writeToParcel
05-18 16:09:49.273 I/ActivityManager( 59): Starting activity: Intent { cmp=parcelable_test.com/.ParcelableTest2 (has extras) }
05-18 16:09:49.453 D/Parcelable( 337): ParcelableTest2
05-18 16:09:49.453 D/Parcelable( 337): createFromParcel
05-18 16:09:49.952 I/ActivityManager( 59): Displayed activity parcelable_test.com/.ParcelableTest2: 562 ms (total 562 ms)
通过log可知程序的运行情况,在mBundle.putParcelable(KEY, mPerson);时,调用了Person类中的public void writeToParcel(Parcel dest, int flags)方法,并向dest写数据,在 Person mPerson = (Person)getIntent().getParcelableExtra(ParcelableTest.KEY);的时候,调用了Person类中的public Person createFromParcel(Parcel source) 方法,创建了一个Person对象,并给这个对象的属性赋值,这里的Parcel source和Parcel dest,是相同的,然后返回这个Person对象。最后就可以打印出mPerson的属性信息了。