From e018a8429c39b83edf95c0597b53b5711a24d1e0 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Thu, 16 Mar 2017 10:23:38 +0100 Subject: [PATCH] ClassifyFragment: onCreateLoader(): Slight refactoring. Reorganize this so we could have multiple loaders. --- .../galaxyzoo/app/ClassifyFragment.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyFragment.java b/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyFragment.java index 98e994b7..0416f366 100644 --- a/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyFragment.java +++ b/app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyFragment.java @@ -48,7 +48,8 @@ */ public class ClassifyFragment extends ItemFragment implements LoaderManager.LoaderCallbacks { - private static final int URL_LOADER = 0; + private static final int LOADER_ID_NEXT_ID = 0; + // We have to hard-code the indices - we can't use getColumnIndex because the Cursor // (actually a SQliteDatabase cursor returned // from our ContentProvider) only knows about the underlying SQLite database column names, @@ -274,7 +275,7 @@ public void update() { if (TextUtils.equals(getItemId(), ItemsContentProvider.URI_PART_ITEM_ID_NEXT)) { /* - * Initializes the CursorLoader. The URL_LOADER value is eventually passed + * Initializes the CursorLoader. The LOADER_ID_NEXT_ID value is eventually passed * to onCreateLoader(). * We use restartLoader(), instead of initLoader(), * so we can refresh this fragment to show a different subject, @@ -295,7 +296,7 @@ public void update() { } //Get the actual ID and other details: - getLoaderManager().restartLoader(URL_LOADER, null, this); + getLoaderManager().restartLoader(LOADER_ID_NEXT_ID, null, this); } } else { //Add, or update, the child fragments already, because we know the Item IDs: @@ -374,13 +375,19 @@ private void updateFromCursor() { addOrUpdateChildFragments(); } - //We only bother using this when we have asked for the "next" item, - //because we want to know its ID. @Override public Loader onCreateLoader(final int loaderId, final Bundle bundle) { - if (loaderId != URL_LOADER) { - return null; + //We only bother using this when we have asked for the "next" item, + //because we want to know its ID. + if (loaderId == LOADER_ID_NEXT_ID) { + return onCreateLoaderGetNextId(); } + + return null; + } + + @Nullable + private Loader onCreateLoaderGetNextId() { final String itemId = getItemId(); if (TextUtils.isEmpty(itemId)) { return null; @@ -417,7 +424,7 @@ public void onLoadFinished(final Loader cursorLoader, final Cursor curso // another item: // See http://stackoverflow.com/questions/14719814/onloadfinished-called-twice // and https://code.google.com/p/android/issues/detail?id=63179 - getLoaderManager().destroyLoader(URL_LOADER); + getLoaderManager().destroyLoader(LOADER_ID_NEXT_ID); } @Override