androidx.loader.app


Interfaces

LoaderManager.LoaderCallbacks

Callback interface for a client to interact with the manager.

Classes

LoaderManager

Static library support version of the framework's android.app.LoaderManager.

Extension functions summary

inline Unit
@MainThread
<D : Any?> LoaderManager.initLoader(
    id: Int,
    loader: Loader<D>,
    crossinline onLoaderReset: () -> Unit,
    crossinline onLoadFinished: (data) -> Unit
)

Ensures a loader is initialized and active.

inline Unit
@MainThread
<D : Any?> LoaderManager.restartLoader(
    id: Int,
    loader: Loader<D>,
    crossinline onLoaderReset: () -> Unit,
    crossinline onLoadFinished: (data) -> Unit
)

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.

Extension functions

LoaderManager.initLoader

@MainThread
inline fun <D : Any?> LoaderManager.initLoader(
    id: Int,
    loader: Loader<D>,
    crossinline onLoaderReset: () -> Unit = {},
    crossinline onLoadFinished: (data) -> Unit
): Unit

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
id: Int

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

loader: Loader<D>

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

crossinline onLoaderReset: () -> Unit = {}

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoaderReset

crossinline onLoadFinished: (data) -> Unit

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoadFinished

LoaderManager.restartLoader

@MainThread
inline fun <D : Any?> LoaderManager.restartLoader(
    id: Int,
    loader: Loader<D>,
    crossinline onLoaderReset: () -> Unit = {},
    crossinline onLoadFinished: (data) -> Unit
): Unit

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
id: Int

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

loader: Loader<D>

The Loader to be used

crossinline onLoaderReset: () -> Unit = {}

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoaderReset

crossinline onLoadFinished: (data) -> Unit

Lambda to call to handle LoaderManager.LoaderCallbacks.onLoadFinished