# androidx.loader.app

*** ** * ** ***

## Interfaces

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.LoaderCallbacks` | Callback interface for a client to interact with the manager. |

## Classes

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager` | Static library support version of the framework's `https://developer.android.com/reference/android/app/LoaderManager.html`. |

## Extension functions summary

|---|---|
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <D : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)( id: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, loader: https://developer.android.com/reference/kotlin/androidx/loader/content/Loader<D>, crossinline onLoaderReset: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html, crossinline onLoadFinished: (data) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Ensures a loader is initialized and active. |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <D : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).restartLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)( id: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, loader: https://developer.android.com/reference/kotlin/androidx/loader/content/Loader<D>, crossinline onLoaderReset: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html, crossinline onLoadFinished: (data) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Starts a new or restarts an existing `https://developer.android.com/reference/kotlin/androidx/loader/content/Loader` in this manager, registers the given `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).restartLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).restartLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` methods to it, and (if the activity/fragment is currently started) starts loading it. |

## Extension functions

### LoaderManager.initLoader

Artifact: [androidx.loader:loader](https://developer.android.com/jetpack/androidx/releases/loader) [View Source](https://cs.android.com/search?q=file:androidx/loader/app/LoaderManager.kt+function:initLoader) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/loader#1.2.0)

```
@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 `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` is used and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used and `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` is ignored.

In either case, the given `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` 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 `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` will be called immediately (inside of this function), so you must be prepared for this to happen.

```kotlin
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | A unique identifier for this loader. Can be whatever you want. Identifiers are scoped to a particular LoaderManager instance. |
| `loader: https://developer.android.com/reference/kotlin/androidx/loader/content/Loader<D>` | The `https://developer.android.com/reference/kotlin/androidx/loader/content/Loader` to be used when no loader is already created with this `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)`. |
| `crossinline onLoaderReset: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html = {}` | Lambda to call to handle `https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoaderReset(androidx.loader.content.Loader<D>)` |
| `crossinline onLoadFinished: (data) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | Lambda to call to handle `https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoadFinished(androidx.loader.content.Loader<D>,D)` |

### LoaderManager.restartLoader

Artifact: [androidx.loader:loader](https://developer.android.com/jetpack/androidx/releases/loader) [View Source](https://cs.android.com/search?q=file:androidx/loader/app/LoaderManager.kt+function:restartLoader) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/loader#1.2.0)

```
@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 `https://developer.android.com/reference/kotlin/androidx/loader/content/Loader` in this manager, registers the given `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).restartLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).restartLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)` 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.

```kotlin
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | A unique identifier for this loader. Can be whatever you want. Identifiers are scoped to a particular LoaderManager instance. |
| `loader: https://developer.android.com/reference/kotlin/androidx/loader/content/Loader<D>` | The `https://developer.android.com/reference/kotlin/androidx/loader/content/Loader` to be used |
| `crossinline onLoaderReset: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html = {}` | Lambda to call to handle `https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoaderReset(androidx.loader.content.Loader<D>)` |
| `crossinline onLoadFinished: (data) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | Lambda to call to handle `https://developer.android.com/reference/kotlin/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoadFinished(androidx.loader.content.Loader<D>,D)` |