Usage of android CursorLoader

Source: Internet
Author: User

The work content is concentrated in the Contact module. This application queries a lot of data. It uses the CursorLoader tool to greatly simplify Code complexity. Android 3.0 provides the Loader mechanism. At that time, google's API was just a brief introduction, but the usage was not provided. Later, due to Performance Optimization under the severe model, R & D's friends found this item very powerful, so they began to notice this powerful tool. CursorLoader is a subclass of Loader. It can be said that it is an upgraded version of Loader. This summary is based on loader. After understanding the principle, you will understand CursorLoader.
First, let's talk about google's official introduction to Loader. Loader is available to activity and fragment. Loader can load data in steps. loader monitors changes in data sources and reports them actively. When configuration changes occur, the re-generated loader automatically connects to the cursor before the change, so that you do not need to check the database again. I am adding that loader can automatically release the queried resources when the application does not use them. These introductions are available from the official document list after android3.0. At that time, I didn't know how to use it. After reading the implementation on the framework side, I was confused: how to use it. Now, this is like activity. We don't know how to start an activity in the framework and manage the activity, but we can still use the activity well. For CursorLoader, we do not need to know the principles in the framework, as long as we make good use of the interface LoaderManager provided by google and the interface LoaderManager for event registration. loaderCallbacks can implement the functions we need.
In fact, CursorLoader can be regarded as a very good query tool and has capabilities that are not available in general queries, as officially introduced by google above. We use the LoaderManager. LoaderCallbacks interface to provide query configuration or use the results returned by the query as appropriate. Better use of CursorLoader focuses on implementing the LoaderManager. LoaderCallbacks interface. Check the methods provided in this interface:

[Java]

Copy codeThe Code is as follows: public interface LoaderCallbacks <D> {
Public Loader <D> onCreateLoader (int id, Bundle args );
Public void onLoadFinished (Loader <D> loader, D data );
Public void onLoaderReset (Loader <D> loader );
}

Public interface LoaderCallbacks <D> {
Public Loader <D> onCreateLoader (int id, Bundle args );
Public void onLoadFinished (Loader <D> loader, D data );
Public void onLoaderReset (Loader <D> loader );
}

The first method onCreateLoader is called when the Loader is created to provide query configuration, such as query address and query project. This method is called during loader initialization, that is, when this interface is registered. The common code is as follows:

[Java]
GetLoaderManager (). initLoader (0, null, this );

GetLoaderManager (). initLoader (0, null, this); the first parameter is the loader ID in the current activity, which is generally 0, the second parameter is usually set to null, and the third parameter implements LoaderManager. loaderCallbacks class, usually the current activity. After the code is executed, onCreateLoader will be executed and then the query will be executed. After the query is completed, onLoadFinished will be executed to do what you need to do. Generally, the query results are used in the second method, for example, transmitted to an adapter for display. The third method onLoaderReset is changed in our configuration, using restartLoader (int, Bundle, LoaderManager. the LoaderCallbacks <D>) method is called after the loader is reinitialized. It is generally used to release reference to the results queried by the loader. To use Loader, you only need to remove references before re-initialization. When you exit activity, you do not need to disable cursor to release resources.
By now, the loader usage has been completed. Remember the usefulness of the above three methods and initialize the loader in a proper place. We can use the Loader to implement our needs. Now let's talk about the relationship between Loader and CursorLoader: Loader is the core and has implemented basic functions; AsyncTaskLoader inherits from Loader, and the main task is to extract time-consuming operations from the main thread; cursorLoader inherits from AsyncTaskLoader, which is a specific class of the generic class and also the most commonly used Loader.
The arrival of Loader brings great convenience to android application development. In the performance optimization of data loading, there is a distributed loading. Before loading data, we need to implement the query in the AsyncQueryHandler class and trigger subsequent queries in its onQueryComplete callback method. The above needs to customize an internal class, a bunch of code, and it gets dizzy. % >_< % Added some judgment in onLoadFinished to Loader, which is convenient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.