/** * Saves {@link RestorableViewState} in a bundle. <b>Should be called from View * onSaveInstanceState(Parcelable) method</b> */ public Parcelable saveViewState(Parcelable superState) { MvpViewStateViewGroupDelegateCallback delegate = (MvpViewStateViewGroupDelegateCallback) delegateCallback; boolean retainingInstanceState = delegate.isRetainInstance(); ViewState viewState = delegate.getViewState(); if (viewState == null) { throw new NullPointerException("ViewState is null! That's not allowed"); } if (retainingInstanceState) { // For next time we call applyViewState() we have to set this flag to true applyViewState = true; return null; } else { ViewStateSavedState state = new ViewStateSavedState(superState); state.setMosbyViewState((RestorableParcelableViewState) delegate.getViewState()); return state; } }
/** * Like the name already suggests. Creates a new viewstate or tries to restore the old one (must * be subclass of {@link RestorableViewState}) by reading the bundle * * @return true, if the viewstate has been restored (in other words restored from parcelable) * (calls {@link BaseMvpViewStateDelegateCallback#onViewStateInstanceRestored(boolean) after * having restored the viewstate}. Otherwise returns false and calls {@link * BaseMvpViewStateDelegateCallback#onNewViewStateInstance()} */ public boolean createOrRestoreViewState(ViewStateSavedState savedState) { if (createOrRestoreCalled) { return false; } createOrRestoreCalled = true; MvpViewStateViewGroupDelegateCallback delegate = (MvpViewStateViewGroupDelegateCallback) delegateCallback; // ViewState already exists if (delegate.getViewState() != null) { applyViewState = true; return false; } if (savedState != null) { ViewState viewState = savedState.getMosbyViewState(); delegate.setViewState(viewState); boolean restoredFromBundle = delegate.getViewState() != null; if (restoredFromBundle) { applyViewState = true; return true; } } // Create view state delegate.setViewState(delegate.createViewState()); if (delegate.getViewState() == null) { throw new NullPointerException( "ViewState is null! Do you return null in createViewState() method?"); } // ViewState not restored, activity / fragment starting first time applyViewState = false; return false; }