Merge "Fix bug 5528574 - "View not attached to window manager" upon orientation change when there is a dialog with ActionMode on" into ics-mr1
diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java
index 7a69419..72e63836 100644
--- a/core/java/android/app/Dialog.java
+++ b/core/java/android/app/Dialog.java
@@ -110,6 +110,8 @@
 
     private Handler mListenersHandler;
 
+    private ActionMode mActionMode;
+
     private final Runnable mDismissAction = new Runnable() {
         public void run() {
             dismissDialog();
@@ -310,6 +312,9 @@
         try {
             mWindowManager.removeView(mDecor);
         } finally {
+            if (mActionMode != null) {
+                mActionMode.finish();
+            }
             mDecor = null;
             mWindow.closeAllPanels();
             onStop();
@@ -952,10 +957,26 @@
         return null;
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * Note that if you override this method you should always call through
+     * to the superclass implementation by calling super.onActionModeStarted(mode).
+     */
     public void onActionModeStarted(ActionMode mode) {
+        mActionMode = mode;
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * Note that if you override this method you should always call through
+     * to the superclass implementation by calling super.onActionModeFinished(mode).
+     */
     public void onActionModeFinished(ActionMode mode) {
+        if (mode == mActionMode) {
+            mActionMode = null;
+        }
     }
 
     /**