Saving the state of fragments?

I have a fragment class. Here it is below:

public class FragmentA extends Fragment { Button button; WebView myWebView; int mCurCheckPosition; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("curChoice", mCurCheckPosition); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) { // Restore last state for checked position. mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) { View mainView = (View) inflater.inflate(R.layout.frag_a, group, false); myWebView = (WebView) mainView.findViewById(R.id.webview); 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.bbc.co.uk"); return mainView; } public class MyWebViewClient extends WebViewClient { /* (non-Java doc) * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String) */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".mp4")) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "video/*"); view.getContext().startActivity(intent); return true; } else { return super.shouldOverrideUrlLoading(view, url); } } } 

}

The problem is, when I go to and from another fragment, the state of the original fragment (which webpage it was on) is lost.

How can I prevent this? I want the state of the web page to remain switched to each fragment and from it.

thanks

+4
source share
2 answers

You must use the WebView.saveState (Bundle state) method in your onSaveInstanceState (Bundle outState) method, and then restore the state using WebView.restoreState (Bundle state) in your onActivityCreated (Bundle savedInstanceState) method

 @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mWebView.saveState(outState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mWebView.restoreState(savedInstanceState); } 

Also remember the fragment life cycle. If you don't know where to restore the state (onCreate, onCreateView, onActivityCreated), take a look at the fragment fragmentation documentation to find out the right place. http://developer.android.com/guide/components/fragments.html

enter image description here

onCreate ()

The system calls this when the fragment is created. As part of your implementation, you must initialize the main components of the fragment that you want to keep when the fragment is paused or stopped, and then resumed.

onCreateView ()

The system calls this when the time for the fragment first displays its user interface. To draw a user interface for your fragment, you must return the view from this method, which is the root of the fragment layout. You can return null if the fragment does not provide a user interface.

onActivityCreated ()

Called when a fragment operation has been created and this fragment view hierarchy has been created. It can be used to perform the final initialization after these parts are in place, for example, to obtain representations or restore state. It is also useful for fragments that use setRetainInstance (boolean) to save their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView (LayoutInflater, ViewGroup, Bundle) and before onStart ().

+5
source

I had the same problem with webView content in fragment. What worked for me hid the current fragment when loading a new one instead of replacing it. This makes the look remain in the previous fragment and not be destroyed. Check out the code below.

protected void showTableFragment (fragment fragment, boolean allowBack, asset table) {

  FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); if (table != null) tableSection = (SectionTable) table; Fragment lastFragment = fragmentManager.findFragmentByTag("first_frag" ); if ( lastFragment != null ) { transaction.hide( lastFragment ); } if ( fragment.isAdded() ) { transaction.show( fragment ); } else { transaction.add( R.id.fragment, fragment, "second_frag" ).setBreadCrumbShortTitle( "second_frag" ); } if ( allowBack ) { transaction.addToBackStack( "second_frag" ); } transaction.commit(); } 
0
source

All Articles