Android Intent Putextra(string, Serializable)
I'm sorry if this Question is already answered, i searched a lot, but i couldn't find any question with my problem. I'm writing an android app which gets data from an internet data
Solution 1:
Your class Server
should implement interface Parcelable
in order for its object to be transferred via bundle.
See the example below, which is available here:
publicclassMyParcelableimplementsParcelable {
privateint mData;
publicintdescribeContents() {
return0;
}
publicvoidwriteToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
publicstatic final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
returnnew MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
returnnew MyParcelable[size];
}
};
privateMyParcelable(Parcel in) {
mData = in.readInt();
}
}
Solution 2:
For any object which needs to be passed via Bundle must implement the Parcelable or Seralizable interface.Intent provides both the interfaces:
putExtra(String name, Parcelable value)
putExtra(String name, Serializable value)
https://developer.android.com/reference/android/content/Intent.html
But it is advisable to use Parcelable as it is written specifically for Android and is light-weight in nature.
Post a Comment for "Android Intent Putextra(string, Serializable)"