原文網址:http://blog.csdn.net/lixiang0522/article/details/8642202
android中的組件間傳遞的對象一般實現Parcelable接口,當然也可以使用java的Serializable接口,前者是android專門設計的,效率更高,下面我們就來實現一個Parcelabel。
1. 創建一個類實現Parcelable接口,具體實現如下:
- package com.hebaijun.testparcelable;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class ParcelableData implements Parcelable{
- private String name;
- private int age;
- public ParcelableData(){
- name = "guest";
- age = 20;
- }
- public ParcelableData(Parcel in){
- //順序要和writeToParcel寫的順序一樣
- name = in.readString();
- age = in.readInt();
- }
- 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;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- // TODO Auto-generated method stub
- dest.writeString(name);
- dest.writeInt(age);
- }
- public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
- public ParcelableData createFromParcel(Parcel in) {
- return new ParcelableData(in);
- }
- public ParcelableData[] newArray(int size) {
- return new ParcelableData[size];
- }
- };
- }
2. 通過下面的方法發送對象。Bundle類也實現了Parcelable接口,一般在android中我們是通過Bundle來封裝數據並進行傳送的。
- Intent intent = new Intent();
- intent.setClass(this, SubActivity.class);
- // 直接添加
- //intent.putExtra("MyData", new ParcelableData());
- // 通過Bundle
- Bundle bundle = new Bundle();
- bundle.putString("MyString", "test bundle");
- bundle.putParcelable("MyData", new ParcelableData());
- intent.putExtras(bundle);
- startActivity(intent);
3. 下面的接收對象的方法。
- //ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
- Bundle bundle = getIntent().getExtras();
- ParcelableData parcelableData = bundle.getParcelable("MyData");
- String testBundleString = bundle.getString("MyString");
- Log.v("string=", testBundleString);
- Log.v("name=", parcelableData.getName());
- Log.v("age=", ""+parcelableData.getAge());
傳輸的對象需要實現序列化:有兩種方式,一種是實現Serializable接口,就是原來的java方式;另外一種是android的Parcelable方式,這個性能可能好一些,我猜的,但是這在需要手動去寫Parcelable接口的實現。
Serializable存數據:
- Person mPerson = new Person();
- mPerson.setName("frankie");
- mPerson.setAge(25);
- Intent mIntent = new Intent(this,ObjectTranDemo1.class);
- Bundle mBundle = new Bundle();
- mBundle.putSerializable(SER_KEY,mPerson);
- mIntent.putExtras(mBundle);
Serializable取數據:
// 獲取啟動該ResultActivity的Intent |
24 |
Intent intent = getIntent(); |
25 |
// 獲取該Intent所攜帶的數據 |
26 |
Bundle bundle = intent.getExtras(); |
27 |
// 從bundle數據包中取出數據 |
28 |
Person person = (Person) bundle.getSerializable( "person" ); |
Parcelable存數據:
- Intent mIntent = new Intent(this,ObjectTranDemo2.class);
- Bundle mBundle = new Bundle();
- mBundle.putParcelable(PAR_KEY, mBook);
- mIntent.putExtras(mBundle);
Parcelable取數據:
Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
參考1:http://blog.csdn.net/Android_Tutor/article/details/5740845