Issue
In my custom view i have some field that I want to restore or create new instance if it will not be restored. Surprisingly I didn't find answer for that
Examle class
class MyView extends View {
private Calendar calendar;
public MyView(Context context) {
super(context);
if(!restoring())
calendar = Calendar.getInstance();
}
@Nullable
@Override
protected Parcelable onSaveInstanceState() {
Bundle b = new Bundle();
b.putParcelable("state", super.onSaveInstanceState());
b.putLong("calendar", calendar.getTimeInMillis());
return b;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle b = ((Bundle) state);
super.onRestoreInstanceState(b.getParcelable("state"));
calendar = Calendar.getInstance();
calendar.setTimeInMillis(b.getLong("calendar"));
}
}
}
Is there some check inside custom view that I can make to determine whether view will be restored?
In Activity
there is Bundle savedInstanceState
passed to onCreate()
I thought of making static boolean but it is not way to go.
Solution
Is there some check inside custom view that I can make to determine whether view will be restored?
Within View
class there does not exist such an API.
The only seam that is given you is onRestoreInstanceState(Parcelable)
, which will "never be called with null
state". Thus, if you end up being in onRestoreInstanceState()
, then you can assume, that the view is being restored. Otherwise it is not.
Answered By - azizbekian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.