Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Loader in Android

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "what is the use of Loader in Android", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the use of Loader in Android" article.

1. Basic methods of Android loader

The loader was introduced from android3.0. It makes it easy to load data asynchronously in activity or fragment. The loader has the following characteristics:

They are valid for every Activity and Fragment.

They provide the ability to load data asynchronously.

They monitor the movements of the data source and transmit new results when the content changes.

When they are recreated due to configuration changes, they are automatically reconnected to the last loader's cursor, so there is no need to re-query the data.

Overview of Loader API

When using the loader, many classes and interfaces are involved, which are summarized in the following table:

Class/Interface

Description

LoaderManager an abstract class associated with an Activity or Fragment that manages instances of one or more loaders. This helps an application manage long-running operations related to the lifecycle of Activity or Fragment. The most common way is to use it with a CursorLoader, while applications can write their own loaders to load other types of data.

There is only one LoaderManager per activity or fragment. 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 (loader)

An abstract class that performs asynchronous data loading. It is the base class of the loader. You can use a typical CursorLoader, but you can also implement your own subclasses. Once loaders are activated, they monitor their data sources and send new results when the data changes. AsyncTaskLoader provides an abstract class that AsyncTask performs asynchronous loading work. A subclass of CursorLoaderAsyncTaskLoader, which queries ContentResolver and returns a Cursor. This class implements the loader's protocol in a standard way for querying cursor, whose cursor queries are executed in background threads through AsyncTaskLoader without blocking the interface. This loader is used to load data asynchronously from an ContentProvider. By contrast, executing a managed query through the API of fragment or activity is not good.

Start a loader

LoaderManager manages one or more loaders in an Activiry or Fragment. But each activity or fragment has only one LoaderManager.

You usually initialize a loader in the onCreate () method of activity or in the onActivityCreated () method of fragment. You can create the following:

/ / prepare the loader. You can reconnect an existing one or start a new. GetLoaderManager (). InitLoader (zero null, this)

The initLoader () method takes the following parameters:

A unique ID to mark the loader. In this example, ID is 0. 0.

Optional parameter for initialization of the loader (null in this case).

An implementation of LoaderManager.LoaderCallbacks. Called by LoaderManager to report the loader's events, in this case, the class implements this interface, so it passes itself: this.initLoader () ensures that a loader is initialized and activated. It has two possible outcomes:

If the loader referred to by ID already exists, then the loader will be reused.

If the loader does not exist, initLoader () triggers LoaderManager.LoaderCallbacks 's method onCreateLoader (). This is where you instantiate and return to a new loader.

In both cases, the implementation of the incoming LoaderManager.LoaderCallbacks is tied to the loader. And will be called when the state of the loader changes. If the caller is in the startup state when the method is called, and the requested loader already exists and generates data, the system will immediately call onLoadFinished () (that is, while initLoader () is still executing). So you have to be prepared for this to happen.

Note that initLoader () returns the loader you created, but you don't need to save a reference to it. LoaderManager automatically manages the life of the loader. LoaderManager starts and stops loading actions as needed, and maintains the state of the loader and its associated content. This means that you rarely interact directly with the loader. You usually use LoaderManager.LoaderCallbacks methods to intervene in the data loading process when an event occurs.

Restart the loader

When you use initLoader (), if the loader for the specified ID already exists, it uses this loader. If it does not exist, it will create a new. But sometimes you just want to throw away the old and start new data.

To discard old data, you should use restartLoader (). For example, the following implementation of SearchView.OnQueryTextListener restarts the loader when the user query changes, and the loader needs to be restarted to make a new query using the new search concerns.

Public boolean onQueryTextChanged (String newText) {/ / is called when the search string in the action bar is changed. / / update the search concern, and then restart the load to make a new query using this new concern. MCurFilter =! TextUtils.isEmpty (newText)? NewText: null; getLoaderManager () .restartLoader (0, null, this); return true } LoaderManager.LoaderCallbacks is a callback interface that allows clients to interact with LoaderManager. Loader, which generally refers to CursorLoader, we want to keep data after it stops. This allows applications to keep data between onStop () and onStart () of activity or fragment, so when users return to an application, they don't have to wait for data to load. You use LoaderManager.LoaderCallbacks methods to create new loaders when needed. And tells the application when to stop using the loader data. LoaderManager.LoaderCallbacks contains the following methods: onCreateLoader ()-initializes and returns a new loader with the incoming ID. OnLoadFinished ()-called when a loader has completed its loading process. OnLoaderReset ()-called when a loader is reset and its data is invalid. when you try to manipulate a loader (for example, Through initLoader (), it checks to see if the loader that specifies ID already exists. If it does not exist, it will trigger the LoaderManager.LoaderCallbacks method onCreateLoader (). This is where you create a new loader. Usually this loader is a CursorLoader, but you can also implement your own loader. String mCurFilter ... Public Loader onCreateLoader (int id, Bundle args) {/ / here is called when you need to create a new loader. / / We simply have a loader, so we don't need to care about 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 of / / 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") } the above is about the content of this article, "what is the use of Loader in Android?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report