bundle.putParcelable可以实现传递对象,但是这个对象的类必须实现Parcelable接口才能够使用。

下面是一个简单的在Activity之间传递对象的例子。

parcel实现参数的序列化,把类对象序列化为齐位的二进制流,提高传输的效率。




Parcelable类。最主要的类,也就是我们要传送的对象的类,需要实现Parcelable接口。


import android.os.Parcel;  
import android.os.Parcelable;  
  
public class BenParcelable implements Parcelable {  
  
    public String name;  
  
    public int age;  
  
    public String profession;  
  
    public BenParcelable(String name, int age, String profession) {  
        this.name = name;  
        this.age = age;  
        this.profession = profession;  
    }  
  
    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 String getprofession() {  
        return profession;  
    }  
  
    public void setprofession(String profession) {  
        this.profession = profession;  
    }  
  
    @Override  
    public int describeContents() {  
        return 0;  
    }  
  
    @Override  
    public void writeToParcel(Parcel parcel, int flag) {  
        parcel.writeString(name);  
        parcel.writeInt(age);  
        parcel.writeString(profession);  
    }  
  
    public static final Creator<BenParcelable> CREATOR = new Creator<BenParcelable>() {  
        public BenParcelable createFromParcel(Parcel in) {  
            return new BenParcelable(in);  
        }  
  
        public BenParcelable[] newArray(int size) {  
            return new BenParcelable[size];  
        }  
    };  
  
    private BenParcelable(Parcel in) {  
        name = in.readString();  
        age = in.readInt();  
        profession = in.readString();  
    }  
}



ParcelableActivity类,传递对象的Activity类。



import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
  
public class ParcelableActivity extends Activity {  
  
    private Button myButton;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.main);  
  
        myButton = (Button) findViewById(R.id.myButton);  
  
        myButton.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View arg0) {  
  
                BenParcelable benParcelable = new BenParcelable("BenZeph", 23,  
                        "Java/Android Engineer");  
  
                Intent intent = new Intent();  
  
                intent.setClass(getApplicationContext(),  
                        GetParcelableActivity.class);  
  
                Bundle bundle = new Bundle();  
  
                bundle.putParcelable("Ben", benParcelable);  
  
                intent.putExtras(bundle);  
  
                startActivity(intent);  
  
            }  
        });  
    }  
}



GetParcelableActivity类,接收序列化对象的Activity类。


import android.app.Activity;  
import android.os.Bundle;  
  
public class GetParcelableActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.main);  
  
        BenParcelable parcelable = getIntent().getParcelableExtra("Ben");  
  
        System.out.println(parcelable.getName());  
  
        System.out.println(parcelable.getAge());  
          
        System.out.println(parcelable.getprofession());  
  
    }  
  
}