LoaderManagerKt

Added in 1.2.0-alpha01

public final class LoaderManagerKt


Summary

Public methods

static final void
@MainThread
<D extends Object> initLoader(
    @NonNull LoaderManager receiver,
    int id,
    @NonNull Loader<@NonNull D> loader,
    @NonNull Function0<Unit> onLoaderReset,
    @NonNull Function1<@NonNull data, Unit> onLoadFinished
)

Ensures a loader is initialized and active.

static final void
@MainThread
<D extends Object> restartLoader(
    @NonNull LoaderManager receiver,
    int id,
    @NonNull Loader<@NonNull D> loader,
    @NonNull Function0<Unit> onLoaderReset,
    @NonNull Function1<@NonNull data, Unit> onLoadFinished
)

Starts a new or restarts an existing Loader in this manager, registers the given onLoaderReset and onLoadFinished methods to it, and (if the activity/fragment is currently started) starts loading it.

Public methods

initLoader

@MainThread
public static final void <D extends Object> initLoader(
    @NonNull LoaderManager receiver,
    int id,
    @NonNull Loader<@NonNull D> loader,
    @NonNull Function0<Unit> onLoaderReset,
    @NonNull Function1<@NonNull data, Unit> onLoadFinished
)

Ensures a loader is initialized and active. If the loader doesn't already exist, the given loader is used and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used and loader is ignored.

In either case, the given onLoaderReset and onLoadFinished methods will be associated with the loader, and will be called as the loader state changes. If at the point of call the caller is in its started state, and the requested loader already exists and has generated its data, then callback onLoadFinished will be called immediately (inside of this function), so you must be prepared for this to happen.

LoaderManager.getInstance(this).initLoader(LOADER_ID, MyLoader()) { data ->
// Handle onLoadFinished
}

// Or, if you need an onLoaderReset callback:
LoaderManager.getInstance(this).initLoader(LOADER_ID, MyLoader(), {
// Handle onLoaderReset
}) { data ->
// Handle onLoadFinished
}
Parameters
int id

A unique identifier for this loader. Can be whatever you want. Identifiers are scoped to a particular LoaderManager instance.

@NonNull Loader<@NonNull D> loader

The Loader to be used when no loader is already created with this id.

@NonNull Function0<Unit> onLoaderReset

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoaderReset

@NonNull Function1<@NonNull data, Unit> onLoadFinished

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoadFinished

restartLoader

@MainThread
public static final void <D extends Object> restartLoader(
    @NonNull LoaderManager receiver,
    int id,
    @NonNull Loader<@NonNull D> loader,
    @NonNull Function0<Unit> onLoaderReset,
    @NonNull Function1<@NonNull data, Unit> onLoadFinished
)

Starts a new or restarts an existing Loader in this manager, registers the given onLoaderReset and onLoadFinished methods to it, and (if the activity/fragment is currently started) starts loading it. If a loader with the same id has previously been started it will automatically be destroyed when the new loader completes its work.

LoaderManager.getInstance(this).restartLoader(LOADER_ID, MyLoader()) { data ->
// Handle onLoadFinished
}

// Or, if you need an onLoaderReset callback:
LoaderManager.getInstance(this).restartLoader(LOADER_ID, MyLoader(), {
// Handle onLoaderReset
}) { data ->
// Handle onLoadFinished
}
Parameters
int id

A unique identifier for this loader. Can be whatever you want. Identifiers are scoped to a particular LoaderManager instance.

@NonNull Loader<@NonNull D> loader

The Loader to be used

@NonNull Function0<Unit> onLoaderReset

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoaderReset

@NonNull Function1<@NonNull data, Unit> onLoadFinished

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoadFinished