MainActivity如下:
[java]
package cc.testparcelable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
/**
* Demo描述:
* 利用Parcelable在Activity之間傳遞自定義對象
*/
public class MainActivity extends Activity {
private Button mButton;
private final String key="testparcelable";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Worker worker=new Worker(9527, "zxx");
Bundle bundle=new Bundle();
bundle.putParcelable(key, worker);
Intent intent=new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
AnotherActivity如下:
[java]
package cc.testparcelable;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.os.Parcelable;
public class AnotherActivity extends Activity {
private TextView mTextView;
private final String key="testparcelable";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
init();
}
private void init() {
mTextView=(TextView) findViewById(R.id.textView);
Parcelable parcelable=this.getIntent().getParcelableExtra(key);
Worker worker=(Worker) parcelable;
mTextView.setText("Parcelable傳遞過來的數據為:\n"+"id="+worker.getId()+" name="+worker.getName());
}
}
Worker如下:
[java]
package cc.testparcelable;
import android.os.Parcel;
import android.os.Parcelable;
/**
* 要傳遞自定義類型,應讓自定義類型支持parcelable協議,
*
* 實現步驟如下:
* 1>自定義類型必須實現Parcelable接口,並且實現Parcelable接口的public void writeToParcel(Parcel dest, int flags)方法 .
* 2>自定義類型中必須含有一個名稱為CREATOR的靜態成員,該成員對象要求實現Parcelable.Creator接口及其方法.
*
* Parcelable接口的作用:
* 實現了Parcelable接口的實例可以將自身的狀態信息(狀態信息通常指的是各成員變量的值)寫入Parcel
* 也可以從Parcel中恢復其狀態. 所以使用Parcel用來完成數據的序列化傳遞.
*/
public class Worker implements Parcelable {
private Integer id;
private String name;
public Worker() {
super();
}
public Worker(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Worker [id=" + id + ", name=" + name + "]";
}
/////////////////////////////////////////////////////////////
//對象在Parcelable中的編碼形式
@Override
public int describeContents() {
return 0;
}
//把javaBean中的數據寫到Parcel
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(this.id);
parcel.writeString(this.name);
}
//添加一個靜態成員,名為CREATOR,該對象實現了Parcelable.Creator接口
public static final Parcelable.Creator<Worker> CREATOR = new Parcelable.Creator<Worker>(){
//從Parcel中讀取數據,返回worker對象
@Override
public Worker createFromParcel(Parcel source) {
return new Worker(source.readInt(), source.readString());
}
@Override
public Worker[] newArray(int size) {
return new Worker[size];
}
};
}
main.xml如下:
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Parcelable"
android:layout_centerInParent="true"
/>
</RelativeLayout>
another.xml如下:
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>