Android loader-callback method using loadermanager

Source: Internet
Author: User
Tags baseuri

Loadermanager. loadercallbacks is a callback interface that allows customers to interact with loadmanager.
The loader, especially the cursorloader loader, is expected to be used to save the terminated data, so that the application can either onstop () and onstart () between activities or fragment () when switching between methods, save the data so that when the user returns to the application, they do not need to wait because of data overload. By using the callback method of loadermanager. loadercallbacks (), you will know when to create a new loader and tell the application when it is time to stop using the loader data.

The loadermanager. loadercallbacks interface includes the following methods:

1. oncreateloader () --- instantiate with the given ID and return a new loader object;

2. onloaderfinished () --- this method is called when the previously created loader has completed its loading;

3. onloaderreset () --- Call this method when the previously created loader is reset, which will invalidate the data of the loader.

The following describes these methods in detail.

Oncreateloader

When you want to access a loader (for example, through the initloader () method), it will check whether the loader with the specified ID exists. If it does not exist, it will trigger loadermanager. the oncreateloader () callback method of loadercallbacks. This is where a new loader is created. Typically, a cursorloader is created, but you can implement the subclass of your own loader class.

For example:

// If non-null, this is the current filter the user has provided.
String mcurfilter;
...
Public loader <cursor> oncreateloader (int id, bundle ARGs ){
// This is called when a new loader needs to be created. This
// Sample only has one loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// Currently filtering.
Uri baseuri;
If (mcurfilter! = NULL ){
Baseuri = URI. withappendedpath (contacts. content_filter_uri,
Uri. encode (mcurfilter ));
} Else {
Baseuri = contacts. content_uri;
}

// Now create and return a cursorloader that will take care
// Creating a cursor for the data being displayed.
String select = "(" + contacts. display_name + "notnull) and ("
+ Contacts. has_phone_number + "= 1) and ("
+ Contacts. display_name + "! = ''))";
Return new cursorloader (getactivity (), baseuri,
Contacts_summary_projection, select, null,
Contacts. display_name + "collate localized ASC ");
}

In this column, the oncreateloader () callback method creates a cursorloader. You must use the constructor method to create this cursorloader object, it requires a complete set of information required to query contentprovider. The specific requirements are as follows:

1. Uri --- the location of the content to be obtained (URI );

2. Projection --- a list of columns to be returned. If null is passed, all columns will be returned, Which is inefficient.

3. Selection --- a filter statement for the row to be returned. It uses the SQL WHERE clause format (excluding the where keyword itself ). When null is passed, all row data of the given URI is returned.

4. selectionargs --- You can include "?" In Selection These characters will be replaced by the value sequence in selectionargs. These values will be bound as strings.

5. sortorder --- use the SQL order by clause format (excluding order by itself) to specify how the row records are sorted. If null is passed, the default sorting is used, or unordered.

Onloadfinished

This method is called when the previously created loader has completed its loading. This method is called before the final data provided to the loader is released. At this point in time, you should delete all old data (because the data is about to be released), but you should not release the data yourself, because the loader itself will do this.

Once the loader knows that the application is not using it, it will release the data. For example, if the data is a cursor from the cursorloader object, you should not call the close () method by yourself. If the cursor is placed in the cursoradapter, you should use the swapcursor () method, so that the old cursor object is not closed. For example:

// This is the adapter being used to display the list's data.
Simplecursoradapter madapter;
...

Public void onloadfinished (loader <cursor> loader, cursor data ){
// Swap the new cursor in. (The framework will take care of closing
// Old cursor once we return .)
Madapter. swapcursor (data );
}

 

Onloaderreset

This method is called when the previously created loader is reset. In this way, its data becomes invalid. This callback allows you to find the time when the data is released, so that you can delete the reference to the data before it is released.

Use the swapcursor () method with the null parameter to implement such a call, for example:

// This is the adapter being used to display the list's data.
Simplecursoradapter madapter;
...

Public void onloaderreset (loader <cursor> loader ){
// This is called when the last cursor provided to onloadfinished ()
// Above is about to be closed. We need to make sure we are no
// Longer using it.
Madapter. swapcursor (null );
}

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.