Don't initiate fragment transactions from onDestroy()

b/10918608

Change-Id: I649c5f86e5f77fa2ade33345969a44ffebb463b9
diff --git a/src/com/android/email/activity/setup/AccountCheckSettingsFragment.java b/src/com/android/email/activity/setup/AccountCheckSettingsFragment.java
index 474d7b9..1309dff 100644
--- a/src/com/android/email/activity/setup/AccountCheckSettingsFragment.java
+++ b/src/com/android/email/activity/setup/AccountCheckSettingsFragment.java
@@ -193,8 +193,12 @@
             Utility.cancelTaskInterrupt(mAccountCheckTask);
             mAccountCheckTask = null;
         }
-        // Make doubly sure that the dialog is gone before we're removed from the fragment manager
-        recoverAndDismissCheckingDialog();
+        // Make doubly sure that the dialog isn't pointing at us before we're removed from the
+        // fragment manager
+        final Fragment f = getFragmentManager().findFragmentByTag(CheckingDialog.TAG);
+        if (f != null) {
+            f.setTargetFragment(null, 0);
+        }
     }
 
     /**
@@ -312,6 +316,9 @@
                     getFragmentManager().findFragmentByTag(CheckingDialog.TAG);
         }
         if (mCheckingDialog != null) {
+            // TODO: dismissAllowingStateLoss() can cause the fragment to return later as a zombie
+            // after the fragment manager restores state, if it happens that this call is executed
+            // after the state is saved. Figure out a way to clean this up later. b/11435698
             mCheckingDialog.dismissAllowingStateLoss();
             mCheckingDialog = null;
         }
@@ -700,8 +707,6 @@
             if (mProgressString == null) {
                 mProgressString = getProgressString(getTargetRequestCode());
             }
-            final AccountCheckSettingsFragment target =
-                (AccountCheckSettingsFragment) getTargetFragment();
 
             final ProgressDialog dialog = new ProgressDialog(context);
             dialog.setIndeterminate(true);
@@ -712,7 +717,12 @@
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             dismiss();
-                            target.onCheckingDialogCancel();
+
+                            final AccountCheckSettingsFragment target =
+                                    (AccountCheckSettingsFragment) getTargetFragment();
+                            if (target != null) {
+                                target.onCheckingDialogCancel();
+                            }
                         }
                     });
             return dialog;
@@ -726,7 +736,9 @@
         public void onCancel(DialogInterface dialog) {
             final AccountCheckSettingsFragment target =
                 (AccountCheckSettingsFragment) getTargetFragment();
-            target.onCheckingDialogCancel();
+            if (target != null) {
+                target.onCheckingDialogCancel();
+            }
             super.onCancel(dialog);
         }