I have the class that implements Parcelable interface. That contain HashMap, this HashMap contains bitmap images. I need this HashMap for all my activities. So I used Parcelable. Look on my Parcelable code.
private HashMap<String, Bitmap> map;
public ParcelFullData() {
map = new HashMap();
}
public ParcelFullData(Parcel in) {
map = new HashMap();
readFromParcel(in);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public ParcelFullData createFromParcel(Parcel in) {
return new ParcelFullData(in);
}
public ParcelFullData[] newArray(int size) {
return new ParcelFullData[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(map.size());
for (String s: map.keySet()) {
dest.writeString(s);
dest.writeValue(map.get(s));
}
}
public void readFromParcel(Parcel in) {
int count = in.readInt();
for (int i = 0; i < count; i++) {
map.put(in.readString(), (Bitmap)in.readValue(Bitmap.class.getClassLoader()));
}
}
public Bitmap get(String key) {
return map.get(key);
}
public String[] getKeys() {
Set<String> mapset = map.keySet();
String[] photoName = new String[mapset.size()];
Iterator<String> itr = mapset.iterator();
int index = 0;
while (itr.hasNext()) {
String name = itr.next();
photoName[index] = name;
index++;
}
return photoName;
}
public void put(String key, Bitmap value) {
map.put(key, value);
}
public HashMap<String, Bitmap> getHashMap() {
return map;
}
This will working fine when i pass the one Activity to another Activity.( For example Activity A to Activity B.) But If I pass this to another Activity( For example Activity B to Activity C.) means that HashMap contains no elements. That size is 0.
In Activity_A,
Intent setIntent = new Intent(activity_a, Activity_B.class);
ParcelFullData parcelData = new ParcelFullData();
Bundle b = new Bundle();
b.putParcelable("parcelData", parcelData);
startActivity(setIntent);
In Activity_B
ParcelFullData parcelData = (ParcelFullData)bundle.getParcelable("parcelData");
Parcel parcel = Parcel.obtain();
parcel.writeParcelable(parcelData , 0);
parcelData.writeToParcel(parcel, 0);
Intent setIntent = new Intent(activity_a, Activity_B.class);
Bundle b = new Bundle();
b.putParcelable("parcelData", parcelData);
startActivity(setIntent);
I’m doing the same in Activity C.
How to use resolve this?
Why do you use
bundle? You don’t add this bundle to intent. UseIntent.putExtra(String, Parcelable)and everything will be OK. I pass oneParcelableobject through all activities in my app and it works fine.In first activity:
In second activity: