简体   繁体   中英

onSaveInstanceState and Fragments?

How can I get a fragment to save it's state? I've tried everything but no luck, nothing is working.

Ive tried adding:

public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   myWebView.saveState(outState);
   Log.w("///////", "onSaveInstanceState");

}

But this never gets called.

Any ideas?


I'm now trying to restore the state of the web view:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     View mainView = (View) inflater.inflate(R.layout.frag_b, container, false);
     myWebView = (WebView) mainView.findViewById(R.id.webviewtest);
     myWebView.setWebViewClient(new MyWebViewClient());
     myWebView.getSettings().setPluginsEnabled(true);
     myWebView.getSettings().setBuiltInZoomControls(false); 
     myWebView.getSettings().setSupportZoom(false);
     myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
     myWebView.getSettings().setAllowFileAccess(true); 
     myWebView.getSettings().setDomStorageEnabled(true);
     myWebView.loadUrl("http://www.google.com");   
     myWebView.setId(1);
     myWebView.restoreState(savedInstanceState);
     Log.w("///////", "onCreateView");

    return mainView;
}

But the above doesnt work, any ideas?

Have you looked into this: Fragment's onSaveInstanceState() is never called

You need to make sure to call super.onSaveInstanceState(Bundle) in your FragmentActivity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM