Note that there are some explanatory texts on larger screens.

plurals
  1. POFully retain WebView in fragment after rotation
    text
    copied!<p>I'm currently having a WebView placed in a fragment like this:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContext = container.getContext(); LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout view = (LinearLayout) mInflater.inflate(R.layout.post_view_layout, container,false); view.setBackgroundColor(Color.WHITE); _viewCache = view; _post_WebView = (WebView) view.findViewById(R.id.post_webview); setupWebView(_post_WebView); //This method sets up the webview (javascript mode etc.) and set the url to load return view; } </code></pre> <p>Now whenever I rotate, the activity that contains this fragment gets recreated (I can't use the <code>configChanges="orientation"</code> attribute for the manifest because I'm using ABS) Because of <code>setRetainInstance(true)</code> the fragment, it's layout and all variables get retained fine and all, but whenever I try to scroll or click in the retained WebView I get a <code>BadTokenException</code> (probably because the original activity used for it's context is destroyed during rotation).</p> <p>I could solve this by just recreating the WebView with the new activity context after rotation, but since the webview shows a input form, recreating it after rotation might prove tedious to users. (I already tried saving the state using the <code>saveState</code> and <code>restoreState</code> methods of the WebView, but to no avail)</p> <p>Is there any way to restore or retain the WebView in it's complete pre-rotation state without generating a <code>BadTokenException</code>, just like it would when the <code>configChanges="orientation"</code> attribute has been added?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload