Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App Crashing When Replacing Fragment Before WebView Loads

My app uses a navigation drawer and replaces the fragment on my main activity when a selection is made in the drawer. I have a couple of fragments that show a web view. The problem occurs if I open the drawer and select a different item , thus replacing the fragment with the web view, before the page in the webview has finished loading. When this happens, I get this error:

12-02 08:07:48.605  11745-11745/kyfb.android.kyfb.com.kyfb A/chromium﹕ [FATAL:jni_android.cc(271)] Check failed: false.
12-02 08:07:48.605  11745-11745/kyfb.android.kyfb.com.kyfb A/libc﹕ Fatal signal 6 (SIGABRT), code -6 in tid 11745 (d.kyfb.com.kyfb)

Here is one of the fragments with a webview.

public class AnnualMeetingFragment extends Fragment {

    private WebView web;
    private ProgressBar progressBar;

    private String url;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_ag_facts, null);

        url = "http://www.kyfbnewsroom.com/2013-kentucky-farm-bureau-annual-meeting-program/";

        web = (WebView)rootView.findViewById(R.id.web);
        progressBar = (ProgressBar)rootView.findViewById(R.id.prgPageLoading);

        web.getSettings().setDomStorageEnabled(true);
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setSupportZoom(true);
        web.getSettings().setBuiltInZoomControls(true);
        web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

        web.loadUrl(url);

        web.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView webView, int progress) {
                getActivity().setProgress(progress * 100);
                progressBar.setProgress(progress);
            }
        });

        web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(web, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(web, url);
                progressBar.setProgress(0);
                progressBar.setVisibility(View.GONE);
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {


                if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi") || url.endsWith(".flv")){
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i); //warning no error handling will cause force close if no media player on phone.
                    return true;
                }

                view.loadUrl(url);
                return true;
            }
        });

        return rootView;
    }
}
like image 484
raginggoat Avatar asked Dec 17 '25 05:12

raginggoat


1 Answers

You should destroy the webview when the fragment is detached.

Call the webview.destroy() from the fragment's on detached method.

like image 68
Jacques Massa Avatar answered Dec 19 '25 18:12

Jacques Massa