The loader function is introduced in android3.0, which makes it easy to load data asynchronously in activity or fragment. The loader has the following features:
1. The loader is valid for every activity and fagment;
2. The loader provides the ability to load asynchronous data;
3. The loader monitors data resources and sends new results when the content changes;
4. When the configuration is changed and re-built, the loader automatically reconnects to the final loader cursor. Therefore, you do not need to re-query the data.
There are multiple types of interfaces available for application loaders. The following table provides an overview:
Class/interface |
Description |
Loadermanager |
An abstract class associated with activity and fragment to manage one or more loader instances. This is useful for managing long-time operations combined with activity or fragment lifecycles. The most common is the cursorloader. However, to load other types of data, you can write your own applications for your applications. Each activity or fragment has only one loadermanager, but one loadermanager can have multiple loaders. |
Loadermanager. loadercallbacks |
The callback interface used for customer interaction with loadermanager. For example, you can use the oncreateloader () callback method to create a new loader. |
Loader |
An abstract class that executes asynchronous data loading. This is the base class of the loader. Generally, cursorloader is used, but its own subclass can also be implemented. The loader should monitor the data source during activation and send new results when the content changes. |
Asynctaskloader |
An abstract loader that provides an asynctask object to complete the work. |
Cursorloader |
Asynctaskloader subclass, which queries the contentresolver object and returns a cursor object. This class uses the standard cursor query method to implement the loader protocol. Based on the asynctaskloader object, the background thread is used to execute the cursor query, so that the interface of the application will not be deadlocked. Using this loader is the best way to asynchronously load data from the contentprovider object, rather than executing managed queries through fragment or the APIs of the activity. |
The classes and interfaces in the above table are necessary components for implementing the Application Loader. Each loader you create does not need all of the above content. However, to initialize a loader, you always need to reference A loadermanager object and implement a loader class like the cursorloader class. The following sections show you how to use these classes and interfaces in an application.