Class classes have an onSaveInstanceState() method that returns the Parcelable and onRestoreInstanceState(Parcelable state) to restore state. If I remember correctly, the saved state includes the scroll position. All you have to do is save the Parcelable that you get from calling WebView onSaveInstanceState() in the onSaveInstanceState fragment onSaveInstanceState :
public void onSaveInstanceState(Bundle savedState) { super.onSaveInstanceState(savedState); WebView webView = (WebView) findViewById( ... ); savedState.putParcelable("myKey", webView.onSaveInstanceState()); }
Then, in your onCreateView method onCreateView you restore this state, if available:
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... if (savedInstanceState != null) { WebView webView = (WebView) findViewById( ... ); Parcelable savedState = savedInstanceState.getParcelable("myKey"); if (savedState != null) { webView.onRestoreInstanceState(savedState); } } .... }
source share