Added in API level 11
Deprecated in API level 28

Loader


open class Loader<D : Any!>
kotlin.Any
   ↳ android.content.Loader

A class that performs asynchronous loading of data. While Loaders are active they should monitor the source of their data and deliver new results when the contents change. See android.app.LoaderManager for more detail.

Note on threading: Clients of loaders should as a rule perform any calls on to a Loader from the main thread of their process (that is, the thread the Activity callbacks and other things occur on). Subclasses of Loader (such as AsyncTaskLoader) will often perform their work in a separate thread, but when delivering their results this too should be done on the main thread.

Subclasses generally must implement at least onStartLoading(), onStopLoading(), onForceLoad(), and onReset().

Most implementations should not derive directly from this class, but instead inherit from AsyncTaskLoader.

Summary

Nested classes

An implementation of a ContentObserver that takes care of connecting it to the Loader to have the loader re-load its data when the observer is told it has changed.

abstract

Interface that is implemented to discover when a Loader has been canceled before it finished loading its data.

abstract

Interface that is implemented to discover when a Loader has finished loading its data.

Public constructors
Loader(context: Context!)

Stores away the application context associated with context.

Public methods
open Unit

This function will normally be called for you automatically by android.app.LoaderManager when restarting a Loader.

open Boolean

Attempt to cancel the current load task.

open Unit

Commit that you have actually fully processed a content change that was returned by takeContentChanged.

open String!
dataToString(data: D)

For debugging, converts an instance of the Loader's data class to a string that can be printed.

open Unit

Informs the registered OnLoadCanceledListener that the load has been canceled.

open Unit
deliverResult(data: D)

Sends the result of the load to the registered listener.

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

Print the Loader's state into the given stream.

open Unit

Force an asynchronous load.

open Context!

open Int

open Boolean

Return whether this loader has been abandoned.

open Boolean

Return whether this load has been reset.

open Boolean

Return whether this load has been started.

open Unit

Called when ForceLoadContentObserver detects a change.

open Unit

Registers a class that will receive callbacks when a load is complete.

open Unit

Registers a listener that will receive callbacks when a load is canceled.

open Unit

This function will normally be called for you automatically by android.app.LoaderManager when destroying a Loader.

open Unit

Report that you have abandoned the processing of a content change that was returned by takeContentChanged() and would like to rollback to the state where there is again a pending content change.

Unit

This function will normally be called for you automatically by android.app.LoaderManager when the associated fragment/activity is being started.

open Unit

This function will normally be called for you automatically by android.app.LoaderManager when the associated fragment/activity is being stopped.

open Boolean

Take the current flag indicating whether the loader's content had changed while it was stopped.

open String

open Unit

Remove a listener that was previously added with registerListener.

open Unit

Unregisters a listener that was previously added with registerOnLoadCanceledListener.

Protected methods
open Unit

Subclasses implement this to take care of being abandoned.

open Boolean

Subclasses must implement this to take care of requests to cancelLoad().

open Unit

Subclasses must implement this to take care of requests to forceLoad().

open Unit

Subclasses must implement this to take care of resetting their loader, as per reset().

open Unit

Subclasses must implement this to take care of loading their data, as per startLoading().

open Unit

Subclasses must implement this to take care of stopping their loader, as per stopLoading().

Public constructors

Loader

Added in API level 11
Loader(context: Context!)

Stores away the application context associated with context. Since Loaders can be used across multiple activities it's dangerous to store the context directly; always use getContext() to retrieve the Loader's Context, don't use the constructor argument directly. The Context returned by getContext is safe to use across Activity instances.

Parameters
context Context!: used to retrieve the application context.

Public methods

abandon

Added in API level 11
open fun abandon(): Unit

Deprecated: Deprecated in Java.

This function will normally be called for you automatically by android.app.LoaderManager when restarting a Loader. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader. Tell the Loader that it is being abandoned. This is called prior to reset to have it retain its current data but not report any new data.

cancelLoad

Added in API level 16
Deprecated in API level 28
open fun cancelLoad(): Boolean

Deprecated: Deprecated in Java.

Attempt to cancel the current load task. Must be called on the main thread of the process.

Cancellation is not an immediate operation, since the load is performed in a background thread. If there is currently a load in progress, this method requests that the load be canceled, and notes this is the case; once the background thread has completed its work its remaining state will be cleared. If another load request comes in during this time, it will be held until the canceled load is complete.

Return
Boolean Returns false if the task could not be canceled, typically because it has already completed normally, or because startLoading() hasn't been called; returns true otherwise. When true is returned, the task is still running and the OnLoadCanceledListener will be called when the task completes.

commitContentChanged

Added in API level 18
Deprecated in API level 28
open fun commitContentChanged(): Unit

Deprecated: Deprecated in Java.

Commit that you have actually fully processed a content change that was returned by takeContentChanged. This is for use with rollbackContentChanged() to handle situations where a load is cancelled. Call this when you have completely processed a load without it being cancelled.

dataToString

Added in API level 11
open fun dataToString(data: D): String!

Deprecated: Deprecated in Java.

For debugging, converts an instance of the Loader's data class to a string that can be printed. Must handle a null data.

deliverCancellation

Added in API level 16
Deprecated in API level 28
open fun deliverCancellation(): Unit

Deprecated: Deprecated in Java.

Informs the registered OnLoadCanceledListener that the load has been canceled. Should only be called by subclasses. Must be called from the process's main thread.

deliverResult

Added in API level 11
open fun deliverResult(data: D): Unit

Deprecated: Deprecated in Java.

Sends the result of the load to the registered listener. Should only be called by subclasses. Must be called from the process's main thread.

Parameters
data D: the result of the load

dump

Added in API level 11
open fun dump(
    prefix: String!,
    fd: FileDescriptor!,
    writer: PrintWriter!,
    args: Array<String!>!
): Unit

Deprecated: Deprecated in Java.

Print the Loader'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.

forceLoad

Added in API level 11
open fun forceLoad(): Unit

Deprecated: Deprecated in Java.

Force an asynchronous load. Unlike startLoading() this will ignore a previously loaded data set and load a new one. This simply calls through to the implementation's onForceLoad(). You generally should only call this when the loader is started -- that is, isStarted() returns true.

Must be called from the process's main thread.

getContext

Added in API level 11
open fun getContext(): Context!

Deprecated: Deprecated in Java.

Return
Context! an application context retrieved from the Context passed to the constructor.

getId

Added in API level 11
open fun getId(): Int

Deprecated: Deprecated in Java.

Return
Int the ID of this loader

isAbandoned

Added in API level 11
open fun isAbandoned(): Boolean

Deprecated: Deprecated in Java.

Return whether this loader has been abandoned. In this state, the loader must not report any new data, and must keep its last reported data valid until it is finally reset.

isReset

Added in API level 11
open fun isReset(): Boolean

Deprecated: Deprecated in Java.

Return whether this load has been reset. That is, either the loader has not yet been started for the first time, or its reset() has been called.

isStarted

Added in API level 11
open fun isStarted(): Boolean

Deprecated: Deprecated in Java.

Return whether this load has been started. That is, its startLoading() has been called and no calls to stopLoading() or reset() have yet been made.

onContentChanged

Added in API level 11
open fun onContentChanged(): Unit

Deprecated: Deprecated in Java.

Called when ForceLoadContentObserver detects a change. The default implementation checks to see if the loader is currently started; if so, it simply calls forceLoad(); otherwise, it sets a flag so that takeContentChanged() returns true.

Must be called from the process's main thread.

registerListener

Added in API level 11
open fun registerListener(
    id: Int,
    listener: Loader.OnLoadCompleteListener<D>!
): Unit

Deprecated: Deprecated in Java.

Registers a class that will receive callbacks when a load is complete. The callback will be called on the process's main thread so it's safe to pass the results to widgets.

Must be called from the process's main thread.

registerOnLoadCanceledListener

Added in API level 16
Deprecated in API level 28
open fun registerOnLoadCanceledListener(listener: Loader.OnLoadCanceledListener<D>!): Unit

Deprecated: Deprecated in Java.

Registers a listener that will receive callbacks when a load is canceled. The callback will be called on the process's main thread so it's safe to pass the results to widgets. Must be called from the process's main thread.

Parameters
listener Loader.OnLoadCanceledListener<D>!: The listener to register.

reset

Added in API level 11
open fun reset(): Unit

Deprecated: Deprecated in Java.

This function will normally be called for you automatically by android.app.LoaderManager when destroying a Loader. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader. Resets the state of the Loader. The Loader should at this point free all of its resources, since it may never be called again; however, its startLoading() may later be called at which point it must be able to start running again.

This updates the Loader's internal state so that isStarted() and isReset() will return the correct values, and then calls the implementation's onReset().

Must be called from the process's main thread.

rollbackContentChanged

Added in API level 18
Deprecated in API level 28
open fun rollbackContentChanged(): Unit

Deprecated: Deprecated in Java.

Report that you have abandoned the processing of a content change that was returned by takeContentChanged() and would like to rollback to the state where there is again a pending content change. This is to handle the case where a data load due to a content change has been canceled before its data was delivered back to the loader.

startLoading

Added in API level 11
fun startLoading(): Unit

Deprecated: Deprecated in Java.

This function will normally be called for you automatically by android.app.LoaderManager when the associated fragment/activity is being started. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader. Starts an asynchronous load of the Loader's data. When the result is ready the callbacks will be called on the process's main thread. If a previous load has been completed and is still valid the result may be passed to the callbacks immediately. The loader will monitor the source of the data set and may deliver future callbacks if the source changes. Calling stopLoading will stop the delivery of callbacks.

This updates the Loader's internal state so that isStarted() and isReset() will return the correct values, and then calls the implementation's onStartLoading().

Must be called from the process's main thread.

stopLoading

Added in API level 11
open fun stopLoading(): Unit

Deprecated: Deprecated in Java.

This function will normally be called for you automatically by android.app.LoaderManager when the associated fragment/activity is being stopped. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader.

Stops delivery of updates until the next time startLoading() is called. Implementations should not invalidate their data at this point -- clients are still free to use the last data the loader reported. They will, however, typically stop reporting new data if the data changes; they can still monitor for changes, but must not report them to the client until and if startLoading() is later called.

This updates the Loader's internal state so that isStarted() will return the correct value, and then calls the implementation's onStopLoading().

Must be called from the process's main thread.

takeContentChanged

Added in API level 11
open fun takeContentChanged(): Boolean

Deprecated: Deprecated in Java.

Take the current flag indicating whether the loader's content had changed while it was stopped. If it had, true is returned and the flag is cleared.

toString

Added in API level 11
open fun toString(): String

Deprecated: Deprecated in Java.

Return
String a string representation of the object.

unregisterListener

Added in API level 11
open fun unregisterListener(listener: Loader.OnLoadCompleteListener<D>!): Unit

Deprecated: Deprecated in Java.

Remove a listener that was previously added with registerListener. Must be called from the process's main thread.

unregisterOnLoadCanceledListener

Added in API level 16
Deprecated in API level 28
open fun unregisterOnLoadCanceledListener(listener: Loader.OnLoadCanceledListener<D>!): Unit

Deprecated: Deprecated in Java.

Unregisters a listener that was previously added with registerOnLoadCanceledListener. Must be called from the process's main thread.

Parameters
listener Loader.OnLoadCanceledListener<D>!: The listener to unregister.

Protected methods

onAbandon

Added in API level 11
protected open fun onAbandon(): Unit

Deprecated: Deprecated in Java.

Subclasses implement this to take care of being abandoned. This is an optional intermediate state prior to onReset() -- it means that the client is no longer interested in any new data from the loader, so the loader must not report any further updates. However, the loader must keep its last reported data valid until the final onReset() happens. You can retrieve the current abandoned state with isAbandoned.

onCancelLoad

Added in API level 16
Deprecated in API level 28
protected open fun onCancelLoad(): Boolean

Deprecated: Deprecated in Java.

Subclasses must implement this to take care of requests to cancelLoad(). This will always be called from the process's main thread.

Return
Boolean Returns false if the task could not be canceled, typically because it has already completed normally, or because startLoading() hasn't been called; returns true otherwise. When true is returned, the task is still running and the OnLoadCanceledListener will be called when the task completes.

onForceLoad

Added in API level 11
protected open fun onForceLoad(): Unit

Deprecated: Deprecated in Java.

Subclasses must implement this to take care of requests to forceLoad(). This will always be called from the process's main thread.

onReset

Added in API level 11
protected open fun onReset(): Unit

Deprecated: Deprecated in Java.

Subclasses must implement this to take care of resetting their loader, as per reset(). This is not called by clients directly, but as a result of a call to reset(). This will always be called from the process's main thread.

onStartLoading

Added in API level 11
protected open fun onStartLoading(): Unit

Deprecated: Deprecated in Java.

Subclasses must implement this to take care of loading their data, as per startLoading(). This is not called by clients directly, but as a result of a call to startLoading().

onStopLoading

Added in API level 11
protected open fun onStopLoading(): Unit

Deprecated: Deprecated in Java.

Subclasses must implement this to take care of stopping their loader, as per stopLoading(). This is not called by clients directly, but as a result of a call to stopLoading(). This will always be called from the process's main thread.