package com.lin1987www.app; import android.os.Parcel; import android.os.Parcelable; import java.util.HashMap; import java.util.Map; import de.greenrobot.event.EventBus; public class StickyEventMap implements Parcelable { public final Map stickyEvents = new HashMap(); public StickyEventMap(EventBus eventBus) { Map, Object> map = EventBusUtils.getStickyEvents(eventBus); for (Class objClass : map.keySet()) { Object obj = map.get(objClass); if (obj instanceof Parcelable) { stickyEvents.put(objClass.getName(), (Parcelable) obj); } } } private StickyEventMap(Parcel in) { // Try save all objects who implements Parcelable sticky events try { int size = in.readInt(); for (int i = 0; i < size; i++) { String className = in.readString(); Class objClass = Class.forName(className); Parcelable obj = in.readParcelable(objClass.getClassLoader()); stickyEvents.put(className, obj); } } catch (Throwable e) { throw new RuntimeException(e); } } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { int size = stickyEvents.size(); dest.writeInt(size); for (String className : stickyEvents.keySet()) { dest.writeString(className); dest.writeParcelable(stickyEvents.get(className), 0); } } public static final Creator CREATOR = new Creator() { public StickyEventMap createFromParcel(Parcel source) { return new StickyEventMap(source); } public StickyEventMap[] newArray(int size) { return new StickyEventMap[size]; } }; }