阅读Android源代码时,经常看到使用Parcelable的地方,但是一直没太搞清楚它的作用,今天仔细的看了一下,感觉就是把一堆数据打包,然后传输,然后再分解出来。
public interface Parcelable
如果某个类实现了这个接口,那么它的对象实例可以写入到
Parcel
中,并且能够从中恢复,并且这个类必须要有一个
static
的
field
,并且名称要为
CREATOR
,这个
field
是某个实现了
Parcelable.Creator
接口的类的对象实例。
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();
}
}
public static 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
其有两个成员函数:createFromParcel和newArray。
createFromParcel函数创建实现Parcelable接口的某个类的一个新的对象实例,并且用从给定的Parcel中获取的数据进行实例化,Parcel中的数据是在之前通过Parcelable.writeToParcel方式写入的数据。
newArray函数创建实现Parcelable接口的某个类的一个新的对象数组,数组中的每个项都被初始化为null。
编写了一个简单的程序进行了测试,就是从一个Activity向另外一个Activity传递数据:
PersonMsg.java
package com.example.parcelable;
import android.os.Parcel;
import android.os.Parcelable;
public class PersonMsg implements Parcelable
{
private String name = null;
private int age = 0;
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 int describeContents()
{
return 0;
}
public void writeToParcel(Parcel out, int flags)
{
out.writeString(name);
out.writeInt(age);
}
public static final Parcelable.Creator<PersonMsg> CREATOR =
new Parcelable.Creator<PersonMsg>()
{
public PersonMsg createFromParcel(Parcel in)
{
PersonMsg msg = new PersonMsg();
msg.name = in.readString();
msg.age = in.readInt();
return msg;
}
public PersonMsg[] newArray(int size)
{
return new PersonMsg[size];
}
};
}
ActivityFrom.java
package com.example.parcelable;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ActivityFrom extends Activity
{
private EditText nameEditText = null;
private EditText ageEditText = null;
private Button startButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameEditText = (EditText)findViewById(R.id.name_content);
ageEditText = (EditText)findViewById(R.id.age_content);
startButton = (Button)findViewById(R.id.start);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String name = nameEditText.getEditableText().toString();
int age = Integer.valueOf(ageEditText.getEditableText().toString());
Log.d("name is: ", name);
Log.d("age is: ", age + "");
PersonMsg msg = new PersonMsg();
msg.setName(name);
msg.setAge(age);
Intent intent = new Intent();
intent.setClass(ActivityFrom.this, ActivityTo.class);
intent.putExtra("personmsg", msg);
startActivity(intent);
}
});
}
}
ActivityTo.java
package com.example.parcelable;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ActivityTo extends Activity
{
private EditText nameEditText = null;
private EditText ageEditText = null;
private Button startButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameEditText = (EditText)findViewById(R.id.name_content);
ageEditText = (EditText)findViewById(R.id.age_content);
startButton = (Button)findViewById(R.id.start);
startButton.setVisibility(View.INVISIBLE);
Intent intent = getIntent();
PersonMsg msg = (PersonMsg)intent.getParcelableExtra("personmsg");
String name = msg.getName();
int age = msg.getAge();
nameEditText.setText(name);
ageEditText.setText(age + "");
}
}
main.xml
<?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"
>
<LinearLayout
android:id="@+id/first_row"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
>
<TextView
android:id="@+id/name_label"
android:layout_width="60dip"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Name"
>
</TextView>
<EditText
android:id="@+id/name_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|left"
>
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/second_row"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
>
<TextView
android:id="@+id/age_label"
android:layout_width="60dip"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Age"
>
</TextView>
<EditText
android:id="@+id/age_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|left"
>
</EditText>
</LinearLayout>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center"
android:text="start"
>
</Button>
</LinearLayout>
实验结果:
点击开始: