Android Loader details 3: restart and callback

Source: Internet
Author: User
Tags baseuri

Restart Loader
When you use initLoader (), if the loader with the specified ID already exists, it uses this loader. if it does not exist, it will create a new one. but sometimes you want to discard the old data and start new data.
To discard old data, you should use restartLoader (). for example, the following SearchView. the implementation of OnQueryTextListener restarts the loader when the user's query changes. Therefore, the loader needs to be restarted so that a new query can be conducted with the new search consideration.
[Java]
1. <span style = "font-family: KaiTi_GB2312;"> public boolean onQueryTextChanged (String newText ){
2. // called when the search string in the action bar is changed.
3. // update search overhead, and restart the load to use this new overhead for new queries.
4. mCurFilter =! TextUtils. isEmpty (newText )? NewText: null;
5. getLoaderManager (). restartLoader (0, null, this );
6. return true;
7.} </span>
 
Use LoaderManager callback
LoaderManager. LoaderCallbacks is a callback interface that allows the client to interact with LoaderManager.
The loader generally refers to CursorLoader. We want to keep the data after it is stopped. this allows the application to maintain data between the onStop () and onStart () of the activity or fragment, so when the user returns to an application, they do not need to wait for the data to load. you use LoaderManager. the LoaderCallbacks method creates a new loader as needed and tells the application when to stop using the loader data.
LoaderManager. LoaderCallbacks includes the following methods:
• OnCreateLoader ()-ID passed in with data, initialize and return a new loader.
• OnLoadFinished ()-called when a loader completes its loading process.
• OnLoaderReset ()-called when a loader is reset and its data is invalid.
OnCreateLoader
When you try to operate a loader (for example, through initLoader (), you will check whether the loader with the specified ID already exists. if it does not exist, LoaderManager is triggered. loaderCallbacks method onCreateLoader (). this is where you create a new loader. this loader is usually a CursorLoader, but you can also implement your own loader.
In the following example, the callback method onCreateLoader () creates a CursorLoader. you must use the constructor to create CursorLoader. The constructor must execute the complete information of a query to ContentProvider as a parameter. It is particularly required:
• Uri-The URI of the content to be obtained.
• Projection-a column consisting of columns to be returned will be returned if null is input, but this is inefficient.
• Selection-a filter that indicates which rows are returned. formatted like the SQLWHERE Statement (except WHERE). If null is input, all rows are returned.
• SelectionArgs-can you include some 'in selection '? ', It will be replaced by the value of this parameter. The order of these values appears and '? 'Sequence 1 in selection to. The value will be used as a string.
• SortOrder-how to sort the rows. format it into a sample code similar to the sqlorder by statement (except for no ORDERBY statement). The default order is used for null input. The default order may be unordered.
Example:
[Java]
1. // If non-null, this is the current filter the user has provided.
2. String mCurFilter;
3 ....
4. public Loader <Cursor> onCreateLoader (int id, Bundle args ){
5. // This is called when you need to create a new loader.
6. // we just have a loader, so we don't need to care about the ID.
7. // First, pick the base URI to use depending on whether we are
8. // currently filtering.
9. Uri baseUri;
10. if (mCurFilter! = Null ){
11. baseUri = Uri. withAppendedPath (Contacts. CONTENT_FILTER_URI,
12. Uri. encode (mCurFilter ));
13.} else {
14. baseUri = Contacts. CONTENT_URI;
15 .}
16. // Now create and return a CursorLoader that will take care
17. // creating a Cursor for the data being displayed.
18. String select = "(" + Contacts. DISPLAY_NAME + "NOTNULL) AND ("
19. + Contacts. HAS_PHONE_NUMBER + "= 1) AND ("
20. + Contacts. DISPLAY_NAME + "! = ''))";
21. return new CursorLoader (getActivity (), baseUri,
22. CONTACTS_SUMMARY_PROJECTION, select, null,
23. Contacts. DISPLAY_NAME + "collate localized asc ");
24 .}
 

 

From the column of nkmnkm

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.