LoaderManager


abstract class LoaderManager


Static library support version of the framework's android.app.LoaderManager. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Your activity must derive from androidx.fragment.app.FragmentActivity to use this.

Summary

Nested types

Callback interface for a client to interact with the manager.

Public constructors

Public functions

abstract Unit

Stops and removes the loader with the given ID.

abstract Unit
dump(
    prefix: String,
    fd: FileDescriptor?,
    writer: PrintWriter,
    args: Array<String!>?
)

This function is deprecated.

Use enableDebugLogging to understand the series of operations performed by LoaderManager.

java-static Unit

This function is deprecated.

LoaderManager now respects isLoggable for debug logging, allowing you to use adb shell setprop log.tag.LoaderManager VERBOSE.

java-static LoaderManager

Gets a LoaderManager associated with the given owner, such as a androidx.fragment.app.FragmentActivity or androidx.fragment.app.Fragment.

abstract Loader<D!>?
<D> getLoader(id: Int)

Return the Loader with the given id or null if no matching Loader is found.

Boolean

Returns true if any loaders managed are currently running and have not returned data to the application yet.

abstract Loader<D!>
@MainThread
<D> initLoader(
    id: Int,
    args: Bundle?,
    callback: LoaderManager.LoaderCallbacks<D!>
)

Ensures a loader is initialized and active.

abstract Unit

Mark all Loaders associated with this LoaderManager for redelivery of their current data (if any), waiting for the next time the Loader is started if it is currently stopped.

abstract Loader<D!>
@MainThread
<D> restartLoader(
    id: Int,
    args: Bundle?,
    callback: LoaderManager.LoaderCallbacks<D!>
)

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

Extension functions

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.

Public constructors

LoaderManager

Added in 1.0.0
LoaderManager()

Public functions

destroyLoader

Added in 1.0.0
@MainThread
abstract fun destroyLoader(id: Int): Unit

Stops and removes the loader with the given ID. If this loader had previously reported data to the client through onLoadFinished, a call will be made to onLoaderReset.

Must be called from the process's main thread.

dump

Added in 1.0.0
Deprecated in 1.0.0
abstract fun dump(
    prefix: String,
    fd: FileDescriptor?,
    writer: PrintWriter,
    args: Array<String!>?
): Unit

Print the LoaderManager's state into the given stream.

Parameters
prefix: String

Text to print at the front of each line.

fd: FileDescriptor?

The raw file descriptor that the dump is being sent to.

writer: PrintWriter

A PrintWriter to which the dump is to be set.

args: Array<String!>?

Additional arguments to the dump request.

enableDebugLogging

Added in 1.0.0
Deprecated in 1.2.0-alpha01
java-static fun enableDebugLogging(enabled: Boolean): Unit

Control whether the framework's internal loader manager debugging logs are turned on. If enabled, you will see output in logcat as the framework performs loader operations.

See also
isLoggable

getInstance

Added in 1.0.0
java-static fun <T : LifecycleOwner? & ViewModelStoreOwner?> getInstance(owner: T): LoaderManager

Gets a LoaderManager associated with the given owner, such as a androidx.fragment.app.FragmentActivity or androidx.fragment.app.Fragment.

Parameters
<T : LifecycleOwner? & ViewModelStoreOwner?>

A class that maintains its own android.arch.lifecycle.Lifecycle and android.arch.lifecycle.ViewModelStore. For instance, androidx.fragment.app.FragmentActivity or androidx.fragment.app.Fragment.

owner: T

The owner that should be used to create the returned LoaderManager

Returns
LoaderManager

A valid LoaderManager

getLoader

Added in 1.0.0
abstract fun <D> getLoader(id: Int): Loader<D!>?

Return the Loader with the given id or null if no matching Loader is found.

hasRunningLoaders

Added in 1.0.0
fun hasRunningLoaders(): Boolean

Returns true if any loaders managed are currently running and have not returned data to the application yet.

initLoader

Added in 1.0.0
@MainThread
abstract fun <D> initLoader(
    id: Int,
    args: Bundle?,
    callback: LoaderManager.LoaderCallbacks<D!>
): Loader<D!>

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

In either case, the given callback is 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.

Must be called from the process's main thread.

Parameters
id: Int

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

args: Bundle?

Optional arguments to supply to the loader at construction. If a loader already exists (a new one does not need to be created), this parameter will be ignored and the last arguments continue to be used.

callback: LoaderManager.LoaderCallbacks<D!>

Interface the LoaderManager will call to report about changes in the state of the loader. Required.

markForRedelivery

Added in 1.0.0
abstract fun markForRedelivery(): Unit

Mark all Loaders associated with this LoaderManager for redelivery of their current data (if any), waiting for the next time the Loader is started if it is currently stopped. In cases where no data has yet been delivered, this is effectively a no-op. In cases where data has already been delivered via onLoadFinished, this will ensure that onLoadFinished is called again with the same data.

Call this only if you are implementing a LifecycleOwner where the views/elements that developers are likely to use in onLoadFinished can be created and destroyed multiple times without the LifecycleOwner itself being destroyed. Call this when the views/elements are being destroyed to ensure that the data is redelivered upon recreation.

restartLoader

Added in 1.0.0
@MainThread
abstract fun <D> restartLoader(
    id: Int,
    args: Bundle?,
    callback: LoaderManager.LoaderCallbacks<D!>
): Loader<D!>

Starts a new or restarts an existing android.content.Loader in this manager, registers the callbacks 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. The callback will be delivered before the old loader is destroyed.

Must be called from the process's main thread.

Parameters
id: Int

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

args: Bundle?

Optional arguments to supply to the loader at construction.

callback: LoaderManager.LoaderCallbacks<D!>

Interface the LoaderManager will call to report about changes in the state of the loader. Required.

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