Retry: Set up fragments just after setContentView().

Redo of I9e0ad079 with fix for crash on portrait.

I9e0ad079 crashed if you launched the app on portrait, because contact details
fragments don't exist in the layout.
(It worked when you rotated from landscape to portrait, because the fragment
manager would recrate them even though the layout didin't have them.)

The only change from I9e0ad079 is that we still use onAttachFragment to
initialize details fragments.

* Original CL description:

... except for ContactsUnavailableFragment, which is not in the layout.
(we dynamically create it.)

It's part of refactoring to prepare for ViewPager.

This also fixes the "mFavoritesFragment and mFrequentFragment are both
StrequentContactListFragment but we always assume StrequentContactListFragment
is Favorites in onAttachFragment" issue.

Change-Id: If30611039d8cdaa8f91676454eba67e89fcbdcc8
diff --git a/src/com/android/contacts/activities/PeopleActivity.java b/src/com/android/contacts/activities/PeopleActivity.java
index 12a17f2..2445a1f 100644
--- a/src/com/android/contacts/activities/PeopleActivity.java
+++ b/src/com/android/contacts/activities/PeopleActivity.java
@@ -90,7 +90,6 @@
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListPopupWindow;
-import android.widget.SearchView;
 import android.widget.Toast;
 
 import java.util.ArrayList;
@@ -208,19 +207,22 @@
         return mProviderStatus == ProviderStatus.STATUS_NORMAL;
     }
 
+    /**
+     * Initialize fragments that are (or may not be) in the layout.
+     *
+     * For the fragments that are in the layout, we initialize them in
+     * {@link #configureContentView(boolean, Bundle)} after inflating the layout.
+     *
+     * However, there are special fragments which may not be in the layout, so we have to do the
+     * initialization here.
+     * The target fragments are:
+     * - {@link ContactDetailFragment} and {@link ContactDetailUpdatesFragment}:  They may not be
+     *   in the layout depending on the configuration.  (i.e. portrait)
+     * - {@link ContactsUnavailableFragment}: We always create it at runtime.
+     */
     @Override
     public void onAttachFragment(Fragment fragment) {
-        if (fragment instanceof DefaultContactBrowseListFragment) {
-            mAllFragment = (DefaultContactBrowseListFragment)fragment;
-            mAllFragment.setOnContactListActionListener(new ContactBrowserActionListener());
-            if (!getWindow().hasFeature(Window.FEATURE_ACTION_BAR)) {
-                mAllFragment.setContextMenuAdapter(
-                        new ContactBrowseListContextMenuAdapter(mAllFragment));
-            }
-        } else if (fragment instanceof GroupBrowseListFragment) {
-            mGroupsFragment = (GroupBrowseListFragment) fragment;
-            mGroupsFragment.setListener(new GroupBrowserActionListener());
-        } else if (fragment instanceof ContactDetailFragment) {
+        if (fragment instanceof ContactDetailFragment) {
             mContactDetailFragment = (ContactDetailFragment) fragment;
             mContactDetailFragment.setListener(mContactDetailFragmentListener);
         } else if (fragment instanceof ContactDetailUpdatesFragment) {
@@ -230,18 +232,6 @@
             mContactsUnavailableFragment.setProviderStatusLoader(mProviderStatusLoader);
             mContactsUnavailableFragment.setOnContactsUnavailableActionListener(
                     new ContactsUnavailableFragmentListener());
-        } else if (fragment instanceof ContactLoaderFragment) {
-            mContactDetailLoaderFragment = (ContactLoaderFragment) fragment;
-            mContactDetailLoaderFragment.setListener(mContactDetailLoaderFragmentListener);
-        } else if (fragment instanceof GroupDetailFragment) {
-            mGroupDetailFragment = (GroupDetailFragment) fragment;
-            mGroupDetailFragment.setListener(mGroupDetailFragmentListener);
-            mGroupDetailFragment.setQuickContact(PhoneCapabilityTester.isUsingTwoPanes(this));
-        } else if (fragment instanceof StrequentContactListFragment) {
-            mFavoritesFragment = (StrequentContactListFragment) fragment;
-            mFavoritesFragment.setListener(mFavoritesFragmentListener);
-            mFavoritesFragment.setDisplayType(DisplayType.STARRED_ONLY);
-            mFavoritesFragment.setQuickContact(PhoneCapabilityTester.isUsingTwoPanes(this));
         }
     }
 
@@ -275,39 +265,6 @@
             return;
         }
 
-        if (createContentView) {
-            setContentView(R.layout.people_activity);
-
-            mFavoritesView = findViewById(R.id.favorites_view);
-            mDetailsView = findViewById(R.id.details_view);
-            mBrowserView = findViewById(R.id.browse_view);
-
-            final FragmentManager fragmentManager = getFragmentManager();
-            mFavoritesFragment = (StrequentContactListFragment) fragmentManager
-                    .findFragmentById(R.id.favorites_fragment);
-            mFrequentFragment = (StrequentContactListFragment) fragmentManager
-                    .findFragmentById(R.id.frequent_fragment);
-            mAllFragment = (DefaultContactBrowseListFragment) fragmentManager
-                    .findFragmentById(R.id.all_fragment);
-            mGroupsFragment = (GroupBrowseListFragment) fragmentManager
-                    .findFragmentById(R.id.groups_fragment);
-            // Hide all tabs (the current tab will later be reshown once a tab is selected)
-            final FragmentTransaction transaction = fragmentManager.beginTransaction();
-            transaction.hide(mAllFragment);
-            transaction.hide(mGroupsFragment);
-
-            if (mFrequentFragment != null) {
-                mFrequentFragment.setDisplayType(DisplayType.FREQUENT_ONLY);
-            }
-            if (mContactDetailFragment != null) {
-                transaction.hide(mContactDetailFragment);
-            }
-            if (mGroupDetailFragment != null) {
-                transaction.hide(mGroupDetailFragment);
-            }
-            transaction.commit();
-        }
-
         if (mRequest.getActionCode() == ContactsRequest.ACTION_VIEW_CONTACT
                 && !PhoneCapabilityTester.isUsingTwoPanes(this)) {
             redirect = new Intent(this, ContactDetailActivity.class);
@@ -318,6 +275,60 @@
             return;
         }
 
+        if (createContentView) {
+            setContentView(R.layout.people_activity);
+
+            final FragmentManager fragmentManager = getFragmentManager();
+
+            // Hide all tabs (the current tab will later be reshown once a tab is selected)
+            final FragmentTransaction transaction = fragmentManager.beginTransaction();
+
+            // Common fragments that exist on both 1 and 2 panes.
+            mFavoritesFragment = getFragment(R.id.favorites_fragment);
+            mFavoritesFragment.setListener(mFavoritesFragmentListener);
+            mFavoritesFragment.setDisplayType(DisplayType.STARRED_ONLY);
+
+            mAllFragment = getFragment(R.id.all_fragment);
+            mAllFragment.setOnContactListActionListener(new ContactBrowserActionListener());
+            if (!getWindow().hasFeature(Window.FEATURE_ACTION_BAR)) {
+                mAllFragment.setContextMenuAdapter(
+                        new ContactBrowseListContextMenuAdapter(mAllFragment));
+            }
+
+            mGroupsFragment = getFragment(R.id.groups_fragment);
+            mGroupsFragment.setListener(new GroupBrowserActionListener());
+
+            transaction.hide(mAllFragment);
+            transaction.hide(mGroupsFragment);
+
+            if (PhoneCapabilityTester.isUsingTwoPanes(this)) {
+                mFavoritesFragment.setQuickContact(true);
+
+                // Container views for fragments
+                mFavoritesView = getView(R.id.favorites_view);
+                mDetailsView = getView(R.id.details_view);
+                mBrowserView = getView(R.id.browse_view);
+
+                // 2-pane only fragments
+                mFrequentFragment = getFragment(R.id.frequent_fragment);
+                mFrequentFragment.setListener(mFavoritesFragmentListener);
+                mFrequentFragment.setDisplayType(DisplayType.FREQUENT_ONLY);
+                mFrequentFragment.setQuickContact(true);
+
+                mContactDetailLoaderFragment = getFragment(R.id.contact_detail_loader_fragment);
+                mContactDetailLoaderFragment.setListener(mContactDetailLoaderFragmentListener);
+
+                mGroupDetailFragment = getFragment(R.id.group_detail_fragment);
+                mGroupDetailFragment.setListener(mGroupDetailFragmentListener);
+                mGroupDetailFragment.setQuickContact(true);
+
+                transaction.hide(mContactDetailFragment);
+                transaction.hide(mGroupDetailFragment);
+            }
+            transaction.commit();
+            fragmentManager.executePendingTransactions();
+        }
+
         setTitle(mRequest.getActivityTitle());
         ActionBar actionBar = getActionBar();
         mActionBarAdapter = new ActionBarAdapter(this, this);