保存webview片段的状态

时间:2012-08-06 11:12:32

标签: android

我有一个加载在webview中的片段,当我更改片段并返回到此片段时,用户在片段中浏览过的网页丢失了,我做错了什么?

问题出现在我的角色中,还是我在改变片段?我使用网络视图客户端是否重要?

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

}

}

1 个答案:

答案 0 :(得分:0)

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

Changing fragments and saving web view states - going mad