1

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?

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?

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);
        }
    }

}

}

4

1 回答 1

0

请参阅您的其他问题以获取适用于此的可能解决方案:

更改片段并保存 Web 视图状态 - 发疯

于 2013-04-30T06:46:39.220 回答