Prevent raw contact picker dialog from flashing

If the dialog is currently shown and the loader calls onLoadFinished
again, update the cursor without doing another fragment transaction.

Test:
Followed repro steps in bug, confirming that the dialog doesn't
flash in and out, along with debug log statements to confirm
it goes through the setCursor call.

Bug: 32096586
Change-Id: Ia021c66e65adb6b4e34b0d4e3a0e521a31e3086f
diff --git a/src/com/android/contacts/editor/PickRawContactDialogFragment.java b/src/com/android/contacts/editor/PickRawContactDialogFragment.java
index 20e8f35..484747d 100644
--- a/src/com/android/contacts/editor/PickRawContactDialogFragment.java
+++ b/src/com/android/contacts/editor/PickRawContactDialogFragment.java
@@ -105,6 +105,7 @@
     private Cursor mCursor;
     // Uri for the whole Contact.
     private Uri mUri;
+    private CursorAdapter mAdapter;
     private MaterialPalette mMaterialPalette;
 
     public static PickRawContactDialogFragment getInstance(Uri uri, Cursor cursor,
@@ -119,12 +120,12 @@
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
-        final CursorAdapter adapter = new RawContactAccountListAdapter(getContext(), mCursor);
+        mAdapter = new RawContactAccountListAdapter(getContext(), mCursor);
         builder.setTitle(R.string.contact_editor_pick_raw_contact_dialog_title);
-        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
+        builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
-                final long rawContactId = adapter.getItemId(which);
+                final long rawContactId = mAdapter.getItemId(which);
                 final Intent intent = EditorIntents.createEditContactIntentForRawContact(
                         getActivity(), mUri, rawContactId, mMaterialPalette);
                 intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
@@ -146,7 +147,10 @@
         mUri = uri;
     }
 
-    private void setCursor(Cursor cursor) {
+    public void setCursor(Cursor cursor) {
+        if (mAdapter != null) {
+            mAdapter.swapCursor(cursor);
+        }
         mCursor = cursor;
     }