Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 071970d9 authored by Steve Block's avatar Steve Block
Browse files

Fix WebViewFragment to avoid detroying the WebView too early

We must not call WebView.destroy() until after the WebView has been
removed from the view hierarchy.

Bug: 4974517
Change-Id: I33b3a9d4ec098e2ab50626cb8906da7697ff2a33
parent 54fa6196
Loading
Loading
Loading
Loading
+21 −4
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.webkit.WebView;
 */
public class WebViewFragment extends Fragment {
    private WebView mWebView;
    private boolean mIsWebViewAvailable;

    public WebViewFragment() {
    }
@@ -40,7 +41,11 @@ public class WebViewFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (mWebView != null) {
            mWebView.destroy();
        }
        mWebView = new WebView(getActivity());
        mIsWebViewAvailable = true;
        return mWebView;
    }

@@ -63,19 +68,31 @@ public class WebViewFragment extends Fragment {
    }

    /**
     * Called when the view has been detached from the fragment. Destroys the WebView.
     * Called when the WebView has been detached from the fragment.
     * The WebView is no longer available after this time.
     */
    @Override
    public void onDestroyView() {
        mIsWebViewAvailable = false;
        super.onDestroyView();
    }

    /**
     * Called when the fragment is no longer in use. Destroys the internal state of the WebView.
     */
    @Override
    public void onDestroy() {
        if (mWebView != null) {
            mWebView.destroy();
            mWebView = null;
        super.onDestroyView();
        }
        super.onDestroy();
    }

    /**
     * Gets the WebView.
     */
    public WebView getWebView() {
        return mWebView;
        return mIsWebViewAvailable ? mWebView : null;
    }
}