實際開發中偶爾會有這樣的需求,在Activity跳轉的時候傳遞一個實體類對象。廢話少說,直接演示源碼。
其中實體類要實現Parcelable序列化接口,其實例可以從一個包中寫入和恢復。實現Parcelable接口的類還必須有一個名爲CREATOR的靜態字段,它是實現Parcelable的對象。
實體類的模板大概就是這樣:
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();
}
}
好了直接演示我隨意寫的實體類:
/**
* Created by Layne_Yao on 2018-2-26 下午4:27:00.
* CSDN:http://blog.csdn.net/Jsagacity
*/
public class Person implements Parcelable {
private String name;
private int age;
private String sex;
private boolean isMarry;
public Person(String name, int age, String sex, boolean isMarry) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.isMarry = isMarry;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean isMarry() {
return isMarry;
}
public void setMarry(boolean isMarry) {
this.isMarry = isMarry;
}
public Person(Parcel in) {
name = in.readString();
age = in.readInt();
sex = in.readString();
//將byte再轉化回布爾值
isMarry = in.readByte() != 0;
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(sex);
//布爾值這裏是將之轉化成byte進行序列化
dest.writeByte((byte) (isMarry ? 1 : 0));
}
}
主佈局就一個跳轉按鈕:
<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"
tools:context="com.itman.skipdemo.MainActivity" >
<Button
android:id="@+id/btnSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="跳轉" />
</RelativeLayout>
MainActivity中代碼:
public class MainActivity extends ActionBarActivity {
private Button btnSkip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSkip = (Button) findViewById(R.id.btnSkip);
btnSkip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Person person = new Person("Yorkie", 18, "female", false);
Intent intent = new Intent(MainActivity.this,
FirstActivity.class);
intent.putExtra("person", person);
startActivity(intent);
}
});
}
}
傳遞對象時,我發現網上一些做法是將對象通過Bundle的putParcelable(key, value)方法存進Bundle對象裏面,而後再將Bundle對象存進Intent裏面進行傳遞。但是我發現Intent也有存儲通過Parcelable序列化對象的方法:
沒有深究有何不同,演示了一下可以就行,如有不妥的可以指正出來。
接下來繼續,跳轉後頁面就一個顯示文本:
<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"
tools:context="com.itman.skipdemo.FirstActivity" >
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello_world"
android:textSize="25sp" />
</RelativeLayout>
FirstActivity的代碼:
public class FirstActivity extends ActionBarActivity {
private TextView tvContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
tvContent = (TextView) findViewById(R.id.tvContent);
Person person = (Person) getIntent().getExtras().get("person");
String info = "姓名:"+person.getName()+"\n年齡:"+person.getAge()+"\n性別:"+person.getSex()+"\n婚姻狀況:"+person.isMarry();
tvContent.setText(info);
}
}
運行結果,跳轉過後正常打印出對象: