Fix rotation losing track of new group creation.

Re-add listener when re-created and only track when group is finally
created.

Bug: 32771896
Test: Follow repro steps and verify that toast does appear.
Change-Id: Ie56585032eb62b5daf587df06eab1f9785631caa
diff --git a/src/com/android/contacts/editor/GroupMembershipView.java b/src/com/android/contacts/editor/GroupMembershipView.java
index a76e408..a2bd2e9 100644
--- a/src/com/android/contacts/editor/GroupMembershipView.java
+++ b/src/com/android/contacts/editor/GroupMembershipView.java
@@ -17,6 +17,7 @@
 package com.android.contacts.editor;
 
 import android.app.Activity;
+import android.app.FragmentManager;
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
@@ -161,6 +162,18 @@
     private boolean mDefaultGroupVisibilityKnown;
     private boolean mDefaultGroupVisible;
     private boolean mCreatedNewGroup;
+    private GroupNameEditDialogFragment mGroupNameEditDialogFragment;
+    private GroupNameEditDialogFragment.Listener mListener =
+            new GroupNameEditDialogFragment.Listener() {
+                @Override
+                public void onGroupNameEditCancelled() {
+                }
+
+                @Override
+                public void onGroupNameEditCompleted(String name) {
+                    mCreatedNewGroup = true;
+                }
+            };
 
     private String mNoGroupString;
     private int mPrimaryTextColor;
@@ -185,6 +198,15 @@
         setFocusableInTouchMode(true);
     }
 
+    private void setGroupNameEditDialogFragment() {
+        final FragmentManager fragmentManager = ((Activity) getContext()).getFragmentManager();
+        mGroupNameEditDialogFragment = (GroupNameEditDialogFragment)
+                fragmentManager.findFragmentByTag(TAG_CREATE_GROUP_FRAGMENT);
+        if (mGroupNameEditDialogFragment != null) {
+            mGroupNameEditDialogFragment.setListener(mListener);
+        }
+    }
+
     @Override
     public void setEnabled(boolean enabled) {
         super.setEnabled(enabled);
@@ -240,6 +262,7 @@
         mDefaultGroupVisibilityKnown = false;
         mCreatedNewGroup = false;
         updateView();
+        setGroupNameEditDialogFragment();
     }
 
     private void updateView() {
@@ -453,24 +476,12 @@
     private void createNewGroup() {
         UiClosables.closeQuietly(mPopup);
         mPopup = null;
-
-        final GroupNameEditDialogFragment dialog =
-                GroupNameEditDialogFragment.newInstanceForCreation(
-                        new AccountWithDataSet(mAccountName, mAccountType, mDataSet), null);
-
-        // If the device is rotated after the dialog is shown, the listener will become null,
-        // so that the popup from GroupMembershipView will not be shown.
-        dialog.setListener(new GroupNameEditDialogFragment.Listener() {
-            @Override
-            public void onGroupNameEditStarted(String groupName) {
-                mCreatedNewGroup = true;
-            }
-            @Override
-            public void onGroupNameEditCancelled() { }
-        });
-        dialog.show(
+        mGroupNameEditDialogFragment =
+                    GroupNameEditDialogFragment.newInstanceForCreation(
+                            new AccountWithDataSet(mAccountName, mAccountType, mDataSet), null);
+        mGroupNameEditDialogFragment.setListener(mListener);
+        mGroupNameEditDialogFragment.show(
                 ((Activity) getContext()).getFragmentManager(),
                 TAG_CREATE_GROUP_FRAGMENT);
     }
-
 }
diff --git a/src/com/android/contacts/group/GroupNameEditDialogFragment.java b/src/com/android/contacts/group/GroupNameEditDialogFragment.java
index 544dd87..da76c68 100644
--- a/src/com/android/contacts/group/GroupNameEditDialogFragment.java
+++ b/src/com/android/contacts/group/GroupNameEditDialogFragment.java
@@ -67,15 +67,15 @@
 
     /** Callbacks for hosts of the {@link GroupNameEditDialogFragment}. */
     public interface Listener {
-        void onGroupNameEditStarted(String name);
         void onGroupNameEditCancelled();
+        void onGroupNameEditCompleted(String name);
 
         public static final Listener None = new Listener() {
             @Override
-            public void onGroupNameEditStarted(String name) { }
+            public void onGroupNameEditCancelled() { }
 
             @Override
-            public void onGroupNameEditCancelled() { }
+            public void onGroupNameEditCompleted(String name) { }
         };
     }
 
@@ -126,6 +126,7 @@
         } else {
             mGroupName = savedInstanceState.getString(ARG_GROUP_NAME);
         }
+
         mGroupId = args.getLong(ARG_GROUP_ID, NO_GROUP_ID);
         mIsInsert = args.getBoolean(ARG_IS_INSERT, true);
         mAccount = getArguments().getParcelable(ARG_ACCOUNT);
@@ -252,7 +253,7 @@
                     name, getActivity().getClass(), callbackAction);
         }
         ContactSaveService.startService(getActivity(), serviceIntent);
-        getListener().onGroupNameEditStarted(name);
+        getListener().onGroupNameEditCompleted(mGroupName);
         dismiss();
     }