11

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?

1 Answers1

5

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

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

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79
  • Is the `onSaveInstanceState()` method at least being called in your Fragment? – telkins Aug 06 '12 at 16:14
  • How do I get the state of the web view back? –  Aug 06 '12 at 16:16
  • Try looking at this: http://stackoverflow.com/questions/1002085/android-webview-handling-orientation-changes You need to call the restoreState() method in your Fragment. – telkins Aug 06 '12 at 16:18
  • Yes I have that: myWebView.restoreState(savedInstanceState); It does not work. –  Aug 06 '12 at 16:22
  • Try moving the WebView code to onActivityCreated(). I haven't done a lot of work with WebView, but I'm assuming you will need to wait for the Activity in order to do anything with the WebView. – telkins Aug 06 '12 at 16:34
  • Also, from reading the docs [http://developer.android.com/reference/android/webkit/WebView.html#restoreState(android.os.Bundle)] it looks like you are supposed to call restoreState() first and no longer restores display data. It looks like there are separate savePicture() and restorePicture() methods for that. **edit:** sorry the SO link parser is failing badly now, go to the restoreState() method in the docs – telkins Aug 06 '12 at 16:36
  • I moved it to onActivityCreated(). No Luck. Restoring state before intialting web view doesnt work either, –  Aug 06 '12 at 16:46