Moving list state management from Activity to Fragment

Change-Id: I80b68149b1615c844092d609c248b870665d868a
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 8a2fd94..e1cab30 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -130,7 +130,6 @@
 
     private static final boolean ENABLE_ACTION_ICON_OVERLAYS = true;
 
-    private static final String LIST_STATE_KEY = "liststate";
     private static final String SHORTCUT_ACTION_KEY = "shortcutAction";
 
     private static final int SUBACTIVITY_NEW_CONTACT = 1;
@@ -378,11 +377,6 @@
     private int  mWritableSourcesCnt;
     private int  mReadOnlySourcesCnt;
 
-    /**
-     * Used to keep track of the scroll state of the list.
-     */
-    private Parcelable mListState = null;
-
     public String mShortcutAction;
 
     /**
@@ -680,13 +674,9 @@
         finish();
     }
 
-    // TODO move this to the configuration object(s)
     @Deprecated
     public void setupListView(ListAdapter adapter, ListView list) {
         mAdapter = (ContactEntryListAdapter)adapter;
-
-        // We manually save/restore the listview state
-        list.setSaveEnabled(false);
     }
 
     /**
@@ -902,22 +892,6 @@
     }
 
     @Override
-    protected void onSaveInstanceState(Bundle icicle) {
-        super.onSaveInstanceState(icicle);
-        // Save list state in the bundle so we can restore it after the QueryHandler has run
-        if (mListView != null) {
-            icicle.putParcelable(LIST_STATE_KEY, mListView.onSaveInstanceState());
-        }
-    }
-
-    @Override
-    protected void onRestoreInstanceState(Bundle icicle) {
-        super.onRestoreInstanceState(icicle);
-        // Retrieve list state. This will be applied after the QueryHandler has run
-        mListState = icicle.getParcelable(LIST_STATE_KEY);
-    }
-
-    @Override
     protected void onStop() {
         super.onStop();
 
@@ -2306,10 +2280,7 @@
     protected void onQueryComplete(Cursor cursor) {
         mAdapter.changeCursor(cursor);
 
-        // Now that the cursor is populated again, it's possible to restore the list state
-        if (mListState != null) {
-            mListView.onRestoreInstanceState(mListState);
-            mListState = null;
-        }
+        // TODO make this triggered by the Loader
+        mListFragment.completeRestoreInstanceState();
     }
 }
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index 6d3b75c..dfeb15e 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -27,6 +27,8 @@
 
 import android.app.Fragment;
 import android.content.Context;
+import android.os.Bundle;
+import android.os.Parcelable;
 import android.text.Editable;
 import android.text.Html;
 import android.text.TextUtils;
@@ -56,6 +58,8 @@
         OnScrollListener, TextWatcher, OnEditorActionListener, OnCloseListener,
         OnFocusChangeListener, OnTouchListener {
 
+    private static final String LIST_STATE_KEY = "liststate";
+
     private boolean mSectionHeaderDisplayEnabled;
     private boolean mPhotoLoaderEnabled;
     private boolean mSearchMode;
@@ -66,6 +70,11 @@
     private ContactEntryListAdapter mAdapter;
     private ListView mListView;
 
+    /**
+     * Used for keeping track of the scroll state of the list.
+     */
+    private Parcelable mListState;
+
     private boolean mLegacyCompatibility;
     private int mDisplayOrder;
     private ContextMenuAdapter mContextMenuAdapter;
@@ -192,14 +201,15 @@
         // them when an A-Z headers is visible.
         mListView.setDividerHeight(0);
 
+        // We manually save/restore the listview state
+        mListView.setSaveEnabled(false);
+
         if (mContextMenuAdapter != null) {
             mListView.setOnCreateContextMenuListener(mContextMenuAdapter);
         }
 
         mAdapter.setContactNameDisplayOrder(mDisplayOrder);
 
-        ((ContactsListActivity)getActivity()).setupListView(mAdapter, mListView);
-
         configurePinnedHeader();
 
         if (isPhotoLoaderEnabled()) {
@@ -225,6 +235,8 @@
                         "<b>" + getQueryString() + "</b>")));
             }
         }
+
+        ((ContactsListActivity)getActivity()).setupListView(mAdapter, mListView);
     }
 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
@@ -338,4 +350,30 @@
         hideSoftKeyboard();
         finish();
     }
+
+    @Override
+    public void onSaveInstanceState(Bundle icicle) {
+        super.onSaveInstanceState(icicle);
+        // Save list state in the bundle so we can restore it after the QueryHandler has run
+        if (mListView != null) {
+            icicle.putParcelable(LIST_STATE_KEY, mListView.onSaveInstanceState());
+        }
+    }
+
+    @Override
+    public void onRestoreInstanceState(Bundle icicle) {
+        super.onRestoreInstanceState(icicle);
+        // Retrieve list state. This will be applied after the QueryHandler has run
+        mListState = icicle.getParcelable(LIST_STATE_KEY);
+    }
+
+    /**
+     * Restore the list state after the adapter is populated.
+     */
+    public void completeRestoreInstanceState() {
+        if (mListState != null) {
+            mListView.onRestoreInstanceState(mListState);
+            mListState = null;
+        }
+    }
 }