Created
February 8, 2016 16:31
-
-
Save StephenVinouze/d37260dc494ef0092a2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Custom view that handles view state restoration properly. | |
* This process is to be used when using non-unique ids which causes the state restoration to lose its reference. | |
* It basically handles the state restoration by creating a SparseArray for each children views thus avoiding the non-unique id issue. | |
* @see <a href="http://trickyandroid.com/saving-android-view-state-correctly/">http://trickyandroid.com/saving-android-view-state-correctly/</a> | |
* Created by StephenVinouze on 05/02/2016. | |
*/ | |
public class CustomViewWithStateRestoration extends ViewGroup { | |
public CustomViewWithStateRestoration(Context context) { | |
super(context); | |
} | |
public CustomViewWithStateRestoration(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
public Parcelable onSaveInstanceState() { | |
Parcelable superState = super.onSaveInstanceState(); | |
SavedState ss = new SavedState(superState); | |
ss.childrenStates = new SparseArray(); | |
for (int i = 0; i < getChildCount(); i++) { | |
getChildAt(i).saveHierarchyState(ss.childrenStates); | |
} | |
return ss; | |
} | |
@Override | |
public void onRestoreInstanceState(Parcelable state) { | |
SavedState ss = (SavedState) state; | |
super.onRestoreInstanceState(ss.getSuperState()); | |
for (int i = 0; i < getChildCount(); i++) { | |
getChildAt(i).restoreHierarchyState(ss.childrenStates); | |
} | |
} | |
@Override | |
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { | |
dispatchFreezeSelfOnly(container); | |
} | |
@Override | |
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { | |
dispatchThawSelfOnly(container); | |
} | |
static class SavedState extends BaseSavedState { | |
SparseArray childrenStates; | |
SavedState(Parcelable superState) { | |
super(superState); | |
} | |
private SavedState(Parcel in, ClassLoader classLoader) { | |
super(in); | |
childrenStates = in.readSparseArray(classLoader); | |
} | |
@Override | |
public void writeToParcel(Parcel out, int flags) { | |
super.writeToParcel(out, flags); | |
out.writeSparseArray(childrenStates); | |
} | |
public static final ClassLoaderCreator<SavedState> CREATOR | |
= new ClassLoaderCreator<SavedState>() { | |
@Override | |
public SavedState createFromParcel(Parcel source, ClassLoader loader) { | |
return new SavedState(source, loader); | |
} | |
@Override | |
public SavedState createFromParcel(Parcel source) { | |
return createFromParcel(null); | |
} | |
public SavedState[] newArray(int size) { | |
return new SavedState[size]; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment