Save State Webview In Fragment
I'd like to have webview which saves state in Fragment. What I want. I want to save state on my webview in fragment when I switch the Fragment by Navigation Drawer. I found some co
Solution 1:
You could simply add this Methods to your Fragment like this:
publicclassZastupovanieextendsSherlockFragment{
Context context;
privateWebView myWebView
@OverridepublicvoidonSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}
@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View v = inflater.inflate(R.layout.zastupovanie, group, false);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Prosím čakajte. Prebieha načítavanie... ", true);
myWebView = (WebView) v.findViewById(R.id.zastupovanie2);
myWebView.setWebViewClient(newWebViewClient() {
@OverridepublicvoidonPageFinished(WebView view, String url) {
pd.dismiss();
}
});
myWebView.getSettings().setBuiltInZoomControls(false);
myWebView.getSettings().setSupportZoom(false);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
myWebView.loadUrl("http://soszm.edupage.org/substitution/?");
return v;
}
}
Solution 2:
Webview in BottomNavigationView
I am using web-view in one of the fragments in BottomViewNavigation. The solution provided by @Rafael T is a solid one. But onSaveInstanceState()
is not called when you navigate to another Fragment in BottomNavigationView. So I saved and restore the state of Webview like this.
classMyFragmentextendsFragment {
privateBundle webviewstate;
privateWebview mywebview;
// Saving state@OverridepublicvoidonPause(){
super.onPause();
webviewstate = newBundle();
mywebview.saveState(webviewstate);
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
if(webviewstate==null){
myWebView.loadUrl("http://soszm.edupage.org/substitution/?"); //Load the page for first time
}else{
myWebView.restoreState(webviewstate); // Restore the state
}
}
}
Post a Comment for "Save State Webview In Fragment"