[英]Saving the state of a webview fragment
I have a fragment that loads in a webview, when I change fragment and go back to this fragment the web page that the user has browsed to in the fragment is lost, what am I doing wrong? 我有一个片段加载到Web视图中,当我更改片段并返回该片段时,用户在该片段中浏览过的网页丢失了,我在做什么错?
Is the problem here in my fragement or is it how I am changing fragments? 这是我的问题吗?还是我正在改变碎片? Does it matter that I am using a web view client? 使用Web View客户端是否重要?
public class FragmentA extends Fragment {
WebView myWebView;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
@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 {
@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);
}
}
}
} }
Please see your other question for a possible solution that will apply to this as well: 请参见您的其他问题,以获取可能的解决方案,该解决方案也将适用于此问题:
Changing fragments and saving web view states - going mad 更改片段并保存Web视图状态-疯了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.