public abstract class BaseLoaderAssistant<D> extends Object implements LoaderAssistant<D>
LoaderAssistant
that may be used to implement loader assistants for
a specific types of results.
public class DetailActivity extends FragmentActivity { // ... activity members private static final int LOADER_ID = 0x01; private LoaderAssistant mLoaderAssistant; @Override protected void onCreateDB(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLoaderAssistant = new DetailLoaderAssistant(this, getLoaderManager()); mLoaderAssistant.initLoader(LOADER_ID); } // Re-starts the cursor loader to load new data from database. private void reloadUi() { mLoaderAssistant.startLoader(LOADER_ID); } // Updates UI of this activity from the specified cursor. final void updateUi(@NonNull Cursor cursor) { // ... update here UI of DetailActivity } final class DetailLoaderAssistant extends BaseLoaderAssistant<Cursor> { DetailLoaderAssistant(@NonNull Context context, @NonNull LoaderManager loaderManager) { super(loaderManager); } @Override public Loader<Cursor> onCreateLoader(@IntRange(from = 0) int loaderId, @Nullable Bundle params) { return new LoaderBuilder(LoremIpsumEntity.class) .projection(LoremIpsum.class) .build(mContext); } @Override protected void onLoadFinished(@IntRange(from = 0) int loaderId, @NonNull Cursor cursor) { updateUi(cursor); } @Override protected void onLoadFailed(@IntRange(from = 0) int loaderId) { // Optional. // Invoked whenever the loaded cursor within onLoadFinished(Loader, Cursor) is null. } @Override protected void onLoaderReset(@NonNull Loader<Cursor> loader) { // Optional. } } }
Modifier and Type | Field and Description |
---|---|
protected android.content.Context |
mContext
Context that may be used to create loaders for this assistant.
|
protected androidx.loader.app.LoaderManager |
mLoaderManager
Manager used to perform loading operations.
|
EMPTY_ARGUMENTS, NO_LOADER_ID
Constructor and Description |
---|
BaseLoaderAssistant(android.content.Context context,
androidx.loader.app.LoaderManager loaderManager)
Creates a new instance of BaseLoaderAssistant for the specified loaderManager.
|
Modifier and Type | Method and Description |
---|---|
boolean |
destroyLoader(int loaderId)
Destroys a loader with the specified loaderId via the associated LoaderManager's
destroyLoader(int) . |
androidx.loader.content.Loader<D> |
getLoader(int loaderId)
Returns a loader with the specified loaderId via the associated LoaderManager's
getLoader(int) . |
androidx.loader.content.Loader<D> |
initLoader(int loaderId,
android.os.Bundle arguments)
Initializes a loader with the specified loaderId via the associated LoaderManager's
initLoader(int, Bundle, LoaderManager.LoaderCallbacks) . |
abstract androidx.loader.content.Loader<D> |
onCreateLoader(int loaderId,
android.os.Bundle arguments) |
void |
onLoaderReset(androidx.loader.content.Loader<D> loader) |
protected void |
onLoadFailed(int loaderId)
Invoked whenever
onLoadFinished(Loader, Object) is fired and the received results is
null . |
protected abstract void |
onLoadFinished(int loaderId,
D result)
Invoked whenever
onLoadFinished(Loader, Object) is fired and the received result
is valid (not null ). |
void |
onLoadFinished(androidx.loader.content.Loader<D> loader,
D result) |
androidx.loader.content.Loader<D> |
restartLoader(int loaderId,
android.os.Bundle arguments)
Re-starts a loader with the specified loaderId via the associated LoaderManager's
restartLoader(int, Bundle, LoaderManager.LoaderCallbacks) . |
androidx.loader.content.Loader<D> |
startLoader(int loaderId,
android.os.Bundle arguments)
Starts a loader with the specified loaderId either via the associated LoaderManager's
initLoader(int, Bundle, LoaderManager.LoaderCallbacks)
or restartLoader(int, Bundle, LoaderManager.LoaderCallbacks)
depending on whether the requested loader is already initialized or not. |
@NonNull protected final android.content.Context mContext
@NonNull protected final androidx.loader.app.LoaderManager mLoaderManager
public BaseLoaderAssistant(@NonNull android.content.Context context, @NonNull androidx.loader.app.LoaderManager loaderManager)
context
- Context that may be used to create loaders for this assistant.loaderManager
- The manager that is used to perform content loading operations.@Nullable public androidx.loader.content.Loader<D> startLoader(int loaderId, @NonNull android.os.Bundle arguments)
LoaderAssistant
initLoader(int, Bundle, LoaderManager.LoaderCallbacks)
or restartLoader(int, Bundle, LoaderManager.LoaderCallbacks)
depending on whether the requested loader is already initialized or not.
This assistant will be used as LoaderManager.LoaderCallbacks
callback.
startLoader
in interface LoaderAssistant<D>
loaderId
- Id of the desired loader to start.arguments
- The desired arguments for the loader. Should be LoaderAssistant.EMPTY_ARGUMENTS
if
loader does not require any arguments.null
if this assistant does not
create loader for the specified id.LoaderAssistant.initLoader(int, Bundle)
,
LoaderAssistant.restartLoader(int, Bundle)
,
LoaderAssistant.destroyLoader(int)
@Nullable public androidx.loader.content.Loader<D> initLoader(int loaderId, @NonNull android.os.Bundle arguments)
LoaderAssistant
initLoader(int, Bundle, LoaderManager.LoaderCallbacks)
.
This assistant will be used as LoaderManager.LoaderCallbacks
callback.
initLoader
in interface LoaderAssistant<D>
loaderId
- Id of the desired loader to initialize.arguments
- The desired arguments for the loader. Should be LoaderAssistant.EMPTY_ARGUMENTS
if
loader does not require any arguments.null
if this assistant does not create loader
for the specified id.LoaderAssistant.startLoader(int, Bundle)
,
LoaderAssistant.restartLoader(int, Bundle)
,
LoaderAssistant.destroyLoader(int)
@Nullable public androidx.loader.content.Loader<D> restartLoader(int loaderId, @NonNull android.os.Bundle arguments)
LoaderAssistant
restartLoader(int, Bundle, LoaderManager.LoaderCallbacks)
.
This assistant will be used as LoaderManager.LoaderCallbacks
callback.
restartLoader
in interface LoaderAssistant<D>
loaderId
- Id of the desired loader to re-start.arguments
- The desired arguments for the loader. Should be LoaderAssistant.EMPTY_ARGUMENTS
if
loader does not require any arguments.null
if this assistant does not create loader
for the specified id.LoaderAssistant.startLoader(int, Bundle)
,
LoaderAssistant.initLoader(int, Bundle)
,
LoaderAssistant.destroyLoader(int)
@Nullable public androidx.loader.content.Loader<D> getLoader(int loaderId)
LoaderAssistant
getLoader(int)
.getLoader
in interface LoaderAssistant<D>
loaderId
- Id of the desired loader to obtain.null
if there is no such loader with the requested id.LoaderAssistant.startLoader(int, Bundle)
,
LoaderAssistant.initLoader(int, Bundle)
,
LoaderAssistant.restartLoader(int, Bundle)
,
LoaderAssistant.destroyLoader(int)
public boolean destroyLoader(int loaderId)
LoaderAssistant
destroyLoader(int)
.destroyLoader
in interface LoaderAssistant<D>
loaderId
- Id of the desired loader to destroy.True
if loader has been destroyed, false
otherwise.LoaderAssistant.startLoader(int, Bundle)
,
LoaderAssistant.initLoader(int, Bundle)
,
LoaderAssistant.restartLoader(int, Bundle)
,
LoaderAssistant.getLoader(int)
public abstract androidx.loader.content.Loader<D> onCreateLoader(int loaderId, @Nullable android.os.Bundle arguments)
onCreateLoader
in interface androidx.loader.app.LoaderManager.LoaderCallbacks<D>
public void onLoadFinished(@NonNull androidx.loader.content.Loader<D> loader, @Nullable D result)
onLoadFinished
in interface androidx.loader.app.LoaderManager.LoaderCallbacks<D>
protected abstract void onLoadFinished(int loaderId, @NonNull D result)
onLoadFinished(Loader, Object)
is fired and the received result
is valid (not null
).loaderId
- Id of the loader for which has been loading finished.result
- The result received in onLoadFinished(Loader, Object)
.protected void onLoadFailed(int loaderId)
onLoadFinished(Loader, Object)
is fired and the received results is
null
.loaderId
- Id of the loader for which has loading failed.