Skip to content

Commit

Permalink
ClassifyFragment: onCreateLoader(): Slight refactoring.
Browse files Browse the repository at this point in the history
Reorganize this so we could have multiple loaders.
  • Loading branch information
murraycu committed Mar 16, 2017
1 parent ab6e3c8 commit e018a84
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/src/main/java/com/murrayc/galaxyzoo/app/ClassifyFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
*/
public class ClassifyFragment extends ItemFragment implements LoaderManager.LoaderCallbacks<Cursor> {

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,
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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<Cursor> 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<Cursor> onCreateLoaderGetNextId() {
final String itemId = getItemId();
if (TextUtils.isEmpty(itemId)) {
return null;
Expand Down Expand Up @@ -417,7 +424,7 @@ public void onLoadFinished(final Loader<Cursor> 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
Expand Down

0 comments on commit e018a84

Please sign in to comment.