From 44bfdd88a74d5935a011bc8671cb075f2047655b Mon Sep 17 00:00:00 2001 From: Svetoslav Ganov Date: Thu, 26 Jan 2012 10:00:18 -0800 Subject: [PATCH] Dialog not dismissed when tearing down dialog fragment. 1. The dismiss implementaton in Dialog was posting a message on the main thread to perform the real dismiss work. The goal of this was to allow calling dismiss() from multiple threads. The side effect of this is that when dialog fragment is dismissed the dialog is not dimissed until the current loop on the main thread is completed. However, during rotation of the screen the current activity has to be restarted, hence all fragments whould be removed. In the destruction process the dialog grament requests from the dialog to dismiss but since this is asynchromous, the code in ActivityThread#handleDestroyActivity detects a leaking window since the dialog window is still not removed and removes that window. Now when the dialog removal message is processed on the next loop we get an exception that the window has already been removed. Now if Dialog#dismiss() is called from the main thread the call goes right though otherwise a message is posted. bug:5911682 Change-Id: I449d6dd75a84c0ff29ea13dac7d163219cc38341 --- core/java/android/app/Dialog.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java index 4a5b8ebd46458..2cc3b02e5080a 100644 --- a/core/java/android/app/Dialog.java +++ b/core/java/android/app/Dialog.java @@ -27,6 +27,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.util.Log; import android.util.TypedValue; @@ -297,7 +298,11 @@ public class Dialog implements DialogInterface, Window.Callback, * that in {@link #onStop}. */ public void dismiss() { - mHandler.post(mDismissAction); + if (Looper.myLooper() == mHandler.getLooper()) { + dismissDialog(); + } else { + mHandler.post(mDismissAction); + } } void dismissDialog() {