Don't add compact editor fragment if it's already there

Bug 20040887

Change-Id: Ib968203e0340cd442e5652376a9e2aa7347994b1
diff --git a/res/layout/compact_contact_editor_activity.xml b/res/layout/compact_contact_editor_activity.xml
index bb89d01..db847b0 100644
--- a/res/layout/compact_contact_editor_activity.xml
+++ b/res/layout/compact_contact_editor_activity.xml
@@ -16,13 +16,7 @@
 -->
 
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/compact_contact_editor_fragment_container"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
-
-    <fragment android:id="@+id/compact_contact_editor_fragment"
-        class="com.android.contacts.editor.CompactContactEditorFragment"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent" />
-
-</FrameLayout>
+    android:layout_height="match_parent"/>
 
diff --git a/res/layout/compact_contact_editor_fragment.xml b/res/layout/compact_contact_editor_fragment.xml
index a6cda4c..696f8db 100644
--- a/res/layout/compact_contact_editor_fragment.xml
+++ b/res/layout/compact_contact_editor_fragment.xml
@@ -16,6 +16,7 @@
 -->
 
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/compact_contact_editor_fragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@color/background_primary"
diff --git a/src/com/android/contacts/activities/CompactContactEditorActivity.java b/src/com/android/contacts/activities/CompactContactEditorActivity.java
index c1c6c40..e850591 100644
--- a/src/com/android/contacts/activities/CompactContactEditorActivity.java
+++ b/src/com/android/contacts/activities/CompactContactEditorActivity.java
@@ -36,6 +36,13 @@
 
         mFragment = (CompactContactEditorFragment) getFragmentManager().findFragmentById(
                 R.id.compact_contact_editor_fragment);
+        if (mFragment == null) {
+            mFragment = new CompactContactEditorFragment();
+            getFragmentManager().beginTransaction()
+                    .add(R.id.compact_contact_editor_fragment_container,
+                            (CompactContactEditorFragment) mFragment)
+                    .commit();
+        }
         mFragment.setListener(mFragmentListener);
 
         final String action = getIntent().getAction();
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index f6c4952..a7686ae 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -647,7 +647,9 @@
             case REQUEST_CODE_ACCOUNTS_CHANGED: {
                 // Bail if the account selector was not successful.
                 if (resultCode != Activity.RESULT_OK) {
-                    mListener.onReverted();
+                    if (mListener != null) {
+                        mListener.onReverted();
+                    }
                     return;
                 }
                 // If there's an account specified, use it.
@@ -993,6 +995,8 @@
         // prompt the user again, then launch the account prompt.
         if (mEditorUtils.shouldShowAccountChangedNotification()) {
             Intent intent = new Intent(mContext, ContactEditorAccountsChangedActivity.class);
+            // Prevent a second instance from being started on rotates
+            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             mStatus = Status.SUB_ACTIVITY;
             startActivityForResult(intent, REQUEST_CODE_ACCOUNTS_CHANGED);
         } else {
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 5b3c7fa..4949729 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -124,7 +124,9 @@
 
     @Override
     public void onExternalEditorRequest(AccountWithDataSet account, Uri uri) {
-        mListener.onCustomEditContactActivityRequested(account, uri, null, false);
+        if (mListener != null) {
+            mListener.onCustomEditContactActivityRequested(account, uri, null, false);
+        }
     }
 
     @Override