1. Basic method of Android loader Loaders were introduced in Android 3.0. They make it easy to asynchronously load data in an activity or fragment. Loaders have the following features:
Loader API Overview When using loaders, many classes and interfaces are involved. We summarize them in the following table:
Each activity or fragment has only one LoaderManager. But a LoaderManager can have multiple loaders. LoaderManager.LoaderCallbacks is a callback interface for clients to interact with LoaderManager. For example, you use the callback method onCreateLoader() to create a new loader. Loader An abstract class that performs asynchronous data loading. It is the base class for loaders. You can use the typical CursorLoader, but you can also implement your own subclasses. Once loaders are activated, they will monitor their data sources and send new results when the data changes. AsyncTaskLoader provides an abstract class that provides an AsyncTask to perform asynchronous loading work. A subclass of CursorLoaderAsyncTaskLoader that queries a ContentResolver and then returns a Cursor. This class implements the loader protocol in a standard way for querying a cursor. Its cursor query is performed in a background thread through AsyncTaskLoader so that it does not block the interface. Using this loader is the best way to load data asynchronously from a ContentProvider. In contrast, performing a managed query through the fragment or activity API will not work. 2. Start a loader LoaderManager manages one or more loaders in an Activity or Fragment. But each activity or fragment has only one LoaderManager. You usually initialize a loader in the onCreate() method of your activity or in the onActivityCreated() method of your fragment. You can create one as follows:
The initLoader() method has the following parameters:
In both cases, the implementation of the passed LoaderManager.LoaderCallbacks is bound to the loader and will be called when the loader state changes. If the caller is in the starting state when calling this method, and the requested loader already exists and has generated data, the system will call onLoadFinished() immediately (that is, while initLoader() is still executing). So you must be prepared for this situation to happen. Note that initLoader() returns the created loader, but you do not need to keep a reference to it. The LoaderManager automatically manages the life of the loader. The LoaderManager starts and stops loading operations when necessary, and maintains the state of the loader and its associated content. This means that you rarely interact with the loader directly. You usually use the methods of LoaderManager.LoaderCallbacks to intervene in the data loading process when certain events occur. 3. Restart the loader When you use initLoader(), if a loader with the specified ID already exists, it uses that loader. If not, it creates a new one. But sometimes you want to discard the old one and start with a new one. To discard old data, you should use restartLoader(). For example, the following implementation of SearchView.OnQueryTextListener restarts the loader when the user's query changes. The loader then needs to be restarted so that a new query can be made using the new search filter.
|
<<: Play with Android nested scrolling
>>: Android background killing series 2: ActivityManagerService and App on-site recovery mechanism
How to disinfect correctly after picking up a pac...
I think when you see the title, you will probably...
1 The world's longest twin-hole highway tunne...
Who would have thought that the starch sausages s...
In the previous issue, we introduced the GAN mode...
The number of advertisements placed on official a...
The sense of smell is one of the most important s...
Introduction to xUtils xUtils was originally deri...
199IT original compilation When it comes to energ...
Compiled by: Gong Zixin Known for its rich, earth...
Content operation refers to the complete operatio...
The Dutch national team is now one of the top four...
The Webb Space Telescope is finally launched! It ...
There is often such a question on the Internet, w...
Black hat blue ocean knowledge paid project, cont...