# LoaderManagerKt

Added in 1.2.0

*** ** * ** ***

<br />

```
public final class LoaderManagerKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread <D extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/loader/app/LoaderManagerKt#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/loader/app/LoaderManager receiver, int id, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/loader/content/Loader<@https://developer.android.com/reference/androidx/annotation/NonNull D> loader, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoaderReset, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull data, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoadFinished )` Ensures a loader is initialized and active. |
| `static final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread <D extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/loader/app/LoaderManagerKt#(androidx.loader.app.LoaderManager).restartLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/loader/app/LoaderManager receiver, int id, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/loader/content/Loader<@https://developer.android.com/reference/androidx/annotation/NonNull D> loader, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoaderReset, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull data, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoadFinished )` Starts a new or restarts an existing `https://developer.android.com/reference/androidx/loader/content/Loader` in this manager, registers the given `https://developer.android.com/reference/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/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. |

## Public methods

### 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
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 `https://developer.android.com/reference/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/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/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/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/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.

```
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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/loader/content/Loader<@https://developer.android.com/reference/androidx/annotation/NonNull D> loader` | The `https://developer.android.com/reference/androidx/loader/content/Loader` to be used when no loader is already created with this `https://developer.android.com/reference/androidx/loader/app/package-summary#(androidx.loader.app.LoaderManager).initLoader(kotlin.Int,androidx.loader.content.Loader,kotlin.Function0,kotlin.Function1)`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoaderReset` | Lambda to call to handle `https://developer.android.com/reference/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoaderReset(androidx.loader.content.Loader<D>)` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull data, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoadFinished` | Lambda to call to handle `https://developer.android.com/reference/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoadFinished(androidx.loader.content.Loader<D>,D)` |

### 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
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 `https://developer.android.com/reference/androidx/loader/content/Loader` in this manager, registers the given `https://developer.android.com/reference/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/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.

```
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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/loader/content/Loader<@https://developer.android.com/reference/androidx/annotation/NonNull D> loader` | The `https://developer.android.com/reference/androidx/loader/content/Loader` to be used |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoaderReset` | Lambda to call to handle `https://developer.android.com/reference/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoaderReset(androidx.loader.content.Loader<D>)` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull data, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoadFinished` | Lambda to call to handle `https://developer.android.com/reference/androidx/loader/app/LoaderManager.LoaderCallbacks#onLoadFinished(androidx.loader.content.Loader<D>,D)` |