public interface Parcelable
android.os.Parcelable
android.os.Parcelable
描述:
parcel ['pɑ:sl] n. 包裹,小包 vt. 打包;捆扎
bundle ['bʌndl] n. 束;捆 vt. 捆 vi. 匆忙离开
Interface for classes whose instances can be written to and restored from a Parcel
. Classes implementing the Parcelable interface must also have a static field called CREATOR
, which is an object implementing the Parcelable.Creator
interface.
A typical implementation of Parcelable is:
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(); } }
借助Bundle
public class MusicData implements Parcelable {
public final static String KEY_MUSIC_DATA = "MusicData";
private final static String KEY_MUSIC_NAME = "MusicName";
private final static String KEY_MUSIC_TIME = "MusicTime";
private final static String KEY_MUSIC_PATH = "MusicPath";
private final static String KEY_MUSIC_ARITST = "MusicAritst";
public String mMusicName;
public int mMusicTime;
public String mMusicPath;
public String mMusicAritst;
public MusicData()
{
mMusicName = "";
mMusicTime = 0;
mMusicPath = "";
mMusicAritst = "";
}
//写入
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString(KEY_MUSIC_NAME, mMusicName);
bundle.putString(KEY_MUSIC_PATH, mMusicPath);
bundle.putString(KEY_MUSIC_ARITST, mMusicAritst);
bundle.putInt(KEY_MUSIC_TIME, mMusicTime);
dest.writeBundle(bundle);
}
//读取
public static final Parcelable.Creator<MusicData> CREATOR = new Parcelable.Creator<MusicData>() {
@Override
public MusicData createFromParcel(Parcel source) {
MusicData Data = new MusicData();
Bundle mBundle = new Bundle();
mBundle = source.readBundle();
Data.mMusicName = mBundle.getString(KEY_MUSIC_NAME);
Data.mMusicTime = mBundle.getInt(KEY_MUSIC_TIME);
Data.mMusicPath = mBundle.getString(KEY_MUSIC_PATH);
Data.mMusicAritst = mBundle.getString(KEY_MUSIC_ARITST);
return Data;
}
@Override
public MusicData[] newArray(int size) {
return new MusicData[size];
}
};
@Override
public int describeContents() {
return 0;
}
}
public final static String KEY_MUSIC_DATA = "MusicData";
private final static String KEY_MUSIC_NAME = "MusicName";
private final static String KEY_MUSIC_TIME = "MusicTime";
private final static String KEY_MUSIC_PATH = "MusicPath";
private final static String KEY_MUSIC_ARITST = "MusicAritst";
public String mMusicName;
public int mMusicTime;
public String mMusicPath;
public String mMusicAritst;
public MusicData()
{
mMusicName = "";
mMusicTime = 0;
mMusicPath = "";
mMusicAritst = "";
}
//写入
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString(KEY_MUSIC_NAME, mMusicName);
bundle.putString(KEY_MUSIC_PATH, mMusicPath);
bundle.putString(KEY_MUSIC_ARITST, mMusicAritst);
bundle.putInt(KEY_MUSIC_TIME, mMusicTime);
dest.writeBundle(bundle);
}
//读取
public static final Parcelable.Creator<MusicData> CREATOR = new Parcelable.Creator<MusicData>() {
@Override
public MusicData createFromParcel(Parcel source) {
MusicData Data = new MusicData();
Bundle mBundle = new Bundle();
mBundle = source.readBundle();
Data.mMusicName = mBundle.getString(KEY_MUSIC_NAME);
Data.mMusicTime = mBundle.getInt(KEY_MUSIC_TIME);
Data.mMusicPath = mBundle.getString(KEY_MUSIC_PATH);
Data.mMusicAritst = mBundle.getString(KEY_MUSIC_ARITST);
return Data;
}
@Override
public MusicData[] newArray(int size) {
return new MusicData[size];
}
};
@Override
public int describeContents() {
return 0;
}
}