I currently have a WebView hosted in a snippet like this:
@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);
Now, when I rotate, the activity that contains this fragment is recreated (I cannot use the configChanges="orientation" attribute for the manifest because I use ABS) Due to the setRetainInstance(true) fragment, its layout and all variables become all more precisely, thatβs all, but whenever I try to scroll or click in a saved WebView, I get a BadTokenException (probably because the original action used for this context is destroyed during rotation).
I could solve this by simply re-creating the WebView with a new activity context after rotation, but since the web view shows the input form, re-creating it after rotation can be tedious for users. (I already tried to save state using the saveState and restoreState methods for WebView, but to no avail)
Is there a way to restore or save a WebView in this pre-rotation state without BadTokenException , as it would if adding the configChanges="orientation" attribute?
source share