From 6fac158552d473a971da51a371664bc5db47dfb2 Mon Sep 17 00:00:00 2001 From: Ming Li Date: Thu, 29 Nov 2012 16:46:28 -0800 Subject: [PATCH] Fix dismiss() causing crash when dialog is detached. Summary: If dismiss is called (especially on the spinner) after the dialog has been detached from the window, it could cause the whole app to crash. This alleviates that situation. App developers should still call dismiss() on the dialog during onPause() since this does not address the leaking window issue, but if they don't, this at least won't crash their app. Test Plan: Tested various situations where rotation happens (during a webview load, and after posting an update). Reviewers: mmarucheck, clang Reviewed By: mmarucheck Differential Revision: https://phabricator.fb.com/D643794 Task ID: 1878623 --- .../src/com/facebook/widget/WebDialog.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/facebook/src/com/facebook/widget/WebDialog.java b/facebook/src/com/facebook/widget/WebDialog.java index 1b9763629d..393f8f70f6 100644 --- a/facebook/src/com/facebook/widget/WebDialog.java +++ b/facebook/src/com/facebook/widget/WebDialog.java @@ -65,6 +65,7 @@ public class WebDialog extends Dialog { private ImageView crossImageView; private FrameLayout contentFrameLayout; private boolean listenerCalled = false; + private boolean isDetached = false; /** * Interface that implements a listener to be called when the user's interaction with the @@ -151,10 +152,24 @@ public void dismiss() { if (webView != null) { webView.stopLoading(); } - if (spinner.isShowing()) { - spinner.dismiss(); + if (!isDetached) { + if (spinner.isShowing()) { + spinner.dismiss(); + } + super.dismiss(); } - super.dismiss(); + } + + @Override + public void onDetachedFromWindow() { + isDetached = true; + super.onDetachedFromWindow(); + } + + @Override + public void onAttachedToWindow() { + isDetached = false; + super.onAttachedToWindow(); } @Override @@ -337,13 +352,17 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e public void onPageStarted(WebView view, String url, Bitmap favicon) { Util.logd(LOG_TAG, "Webview loading URL: " + url); super.onPageStarted(view, url, favicon); - spinner.show(); + if (!isDetached) { + spinner.show(); + } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); - spinner.dismiss(); + if (!isDetached) { + spinner.dismiss(); + } /* * Once web view is fully loaded, set the contentFrameLayout background to be transparent * and make visible the 'x' image.