Challenge:Saving State (slightly different)

Steps
1: Use this converter to implement Parcelable on Class Box or do it manually
[http://www.parcelabler.com/]

  1. Add 2 constants to class BoxDrawingView to track Parent and Child states

  2. Add id to fragment_drag_and_draw.xml

  3. In class BoxDrawingView add methods onSavedInstanceState and onRestoreInstanceState.

Differences: used putParcelableArrayList, getParcelableArrayList , readValues and writeValues

  1.     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];
         }
     };
    
  2.  private static final String PARENTVIEW_SAVED_STATE = "parentview";
     private static final String CHILDVIEW_SAVED_STATE = "childview";
    
  3. android:id="@+id/box_drawing_view"

  4.   @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);
    
     }
1 Like