Steps
1: Use this converter to implement Parcelable on Class Box or do it manually
[http://www.parcelabler.com/]
-
Add 2 constants to class BoxDrawingView to track Parent and Child states
-
Add id to fragment_drag_and_draw.xml
-
In class BoxDrawingView add methods onSavedInstanceState and onRestoreInstanceState.
Differences: used putParcelableArrayList, getParcelableArrayList , readValues and writeValues
-
protected Box(Parcel in) { mOrigin = (PointF) in.readValue(PointF.class.getClassLoader()); mCurrent = (PointF) in.readValue(PointF.class.getClassLoader()); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeValue(mOrigin); dest.writeValue(mCurrent); } @SuppressWarnings("unused") public static final Parcelable.Creator<Box> CREATOR = new Parcelable.Creator<Box>() { @Override public Box createFromParcel(Parcel in) { return new Box(in); } @Override public Box[] newArray(int size) { return new Box[size]; } };
-
private static final String PARENTVIEW_SAVED_STATE = "parentview"; private static final String CHILDVIEW_SAVED_STATE = "childview";
-
android:id="@+id/box_drawing_view"
-
@Override protected Parcelable onSaveInstanceState() { Parcelable viewState = super.onSaveInstanceState(); Bundle bundle = new Bundle(); bundle.putParcelable(PARENTVIEW_SAVED_STATE, viewState); bundle.putParcelableArrayList(CHILDVIEW_SAVED_STATE,( ArrayList<Box >) mBoxen); return bundle; } @Override protected void onRestoreInstanceState(Parcelable state) { Bundle bundle = (Bundle) state; super.onRestoreInstanceState(bundle.getParcelable(PARENTVIEW_SAVED_STATE)); mBoxen = bundle.getParcelableArrayList(CHILDVIEW_SAVED_STATE); }