[컴][안드로이드] savedState 에 자신이 원하는 state 를 추가하는 법

custom SaveState 만들기 / savedState 사용법 / onSaveInstanceState() 에서 savedState 사용하는 방법

 

savedState 에 자신이 원하는 state 를 추가하는 법

 

class C{
static class SavedState extends BaseSavedState {
boolean isExpanded;

SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
isExpanded = in.readInt() != 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(isExpanded ? 1 : 0);
}

public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}


@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();

// {@ref: http://developer.android.com/reference/android/view/View.BaseSavedState.html}
SavedState ss = new SavedState(superState);
ss.isExpanded = isSlideable() ? isExpanded() : mPreservedExpandedState;

return ss;
}



@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());

if (ss.isExpanded) {
expandPane();
} else {
collapsePane();
}
mPreservedExpandedState = ss.isExpanded;
}


}

댓글 없음:

댓글 쓰기