Bundle Object
有两种方法传递Object给Bundle
Method 1
Java: |
package de.padde.test; import android.app.Activity; import android.os.Bundle; import android.os.Parcelable; import android.os.Parcel; public class Test extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if(!savedInstanceState.isEmpty()) { SaveMe state = savedInstanceState.getParcelable("laststate"); } } protected void onPause() { Bundle b = new Bundle(); b.putParcelable("laststate", new SaveMe()); onSaveInstanceState(b); } } class SaveMe implements Parcelable { static final int test = 1234; private int user; private String name; private double score; private SaveMeToo smt; public void writeToParcel(Parcel out, int flags) { out.writeInt(user); out.writeString(name); out.writeDouble(score); out.writeParcelable(smt, flags); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public SaveMe createFromParcel(Parcel in) { return new SaveMe(in); } public SaveMe[] newArray(int size) { return new SaveMe[size]; } }; private SaveMe(Parcel in) { user = in.readInt(); name = in.readString(); score = in.readDouble(); smt = in.readParcelable(getClass().getClassLoader()); } public SaveMe() {} public int describeContents() { return 0; } } class SaveMeToo implements Parcelable { private int moo; public void writeToParcel(Parcel out, int flags) { out.writeInt(moo); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public SaveMeToo createFromParcel(Parcel in) { return new SaveMeToo(in); } public SaveMeToo[] newArray(int size) { return new SaveMeToo[size]; } }; private SaveMeToo(Parcel in) { moo = in.readInt(); } public int describeContents() { return 0; } } |
Method 2
Java: |
package de.padde.test; import java.io.Serializable; import android.app.Activity; import android.os.Bundle; public class Test extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if(!savedInstanceState.isEmpty()) { SaveMe state = (SaveMe) savedInstanceState.getSerializable("laststate"); } } protected void onPause() { Bundle b = new Bundle(); b.putSerializable("laststate", new SaveMe()); onSaveInstanceState(b); } } class SaveMe implements Serializable { private static final long serialVersionUID = 1L; static final int test = 1234; private int user; private String name; private double score; private SaveMeToo smt; public SaveMe() {} } class SaveMeToo implements Serializable { private static final long serialVersionUID = 2L; private int moo; public SaveMeToo() {} } |