From 79d4a2d9e1537727eb1b66c0df91b2b43ab1a9a5 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Tue, 10 Aug 2010 18:21:06 -0700 Subject: [PATCH] Make use of the retaining-loader feature. Don't use restartLoader(). Use initLoader() with stopLoader() when necessary. This allows us to make use of LoaderManager's "retaining" feature. i.e. We won't have to requery when the screen orientation changes. Also, don't start loading in onStart(). Fragment manager seems to kick exisiting loaders after onStart(), so you'll end up getting onLoadFinished() twice. (Looks like I got unluck with this--there was no strong reason to start loading in onStart rather than onResume, but I think we can leave other fragments as-is as long as they don't use loaders.) Seems to be working. Change-Id: Ib4f72098bd0fcbfce284ae6e16055d20ee410cf3 --- .../email/activity/MailboxListFragment.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/com/android/email/activity/MailboxListFragment.java b/src/com/android/email/activity/MailboxListFragment.java index ece0396eb..e3499e614 100644 --- a/src/com/android/email/activity/MailboxListFragment.java +++ b/src/com/android/email/activity/MailboxListFragment.java @@ -45,6 +45,8 @@ import java.security.InvalidParameterException; */ public class MailboxListFragment extends ListFragment implements OnItemClickListener { private static final int LOADER_ID_MAILBOX_LIST = 1; + + private long mLastLoadedAccountId = -1; private long mAccountId = -1; // UI Support @@ -53,7 +55,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList private Callback mCallback = EmptyCallback.INSTANCE; private final MyLoaderCallbacks mMyLoaderCallbacks = new MyLoaderCallbacks(); - private boolean mStarted; + private boolean mResumed; /** * Callback interface that owning activities must implement @@ -122,7 +124,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList return; } mAccountId = accountId; - if (mStarted) { + if (mResumed) { startLoading(); } } @@ -135,12 +137,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Email.LOG_TAG, "MailboxListFragment onStart"); } - getLoaderManager(); // TODO Work around internal bug 2887723. super.onStart(); - mStarted = true; - if (mAccountId != -1) { - startLoading(); - } } /** @@ -152,6 +149,11 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList Log.d(Email.LOG_TAG, "MailboxListFragment onResume"); } super.onResume(); + + mResumed = true; + if (mAccountId != -1) { + startLoading(); + } } @Override @@ -159,6 +161,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Email.LOG_TAG, "MailboxListFragment onPause"); } + mResumed = false; super.onPause(); } @@ -170,7 +173,6 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Email.LOG_TAG, "MailboxListFragment onStop"); } - mStarted = false; super.onStop(); } @@ -200,7 +202,14 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList // Clear the list. (ListFragment will show the "Loading" animation) setListShown(false); - getLoaderManager().restartLoader(LOADER_ID_MAILBOX_LIST, null, mMyLoaderCallbacks); + // If we've already loaded for a different account, discard the previous result and + // start loading again. + // We don't want to use restartLoader(), because if the Loader is retained, we *do* want to + // reuse the previous result. + if ((mLastLoadedAccountId != -1) && (mLastLoadedAccountId != mAccountId)) { + getLoaderManager().stopLoader(LOADER_ID_MAILBOX_LIST); + } + getLoaderManager().initLoader(LOADER_ID_MAILBOX_LIST, null, mMyLoaderCallbacks); } private class MyLoaderCallbacks implements LoaderCallbacks { @@ -217,6 +226,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList if (Email.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Email.LOG_TAG, "MailboxListFragment onLoadFinished"); } + mLastLoadedAccountId = mAccountId; // Save list view state (primarily scroll position) final ListView lv = getListView();