Deep source parsing Android loader, Asynctaskloader, Cursorloader, Loadermanager

Source: Internet
Author: User

If the concepts of loader, Asynctaskloader, Cursorloader, Loadermanager, etc. do not understand or do not know how to use the loader mechanism, See the use of loader and Loadermanager in the Android blog (with source download). This paper mainly studies the source code of loader and its subclasses and Loadermanager by studying the life cycle of loader and its subclasses.

Loader is managed by Loadermanager, Loadermanager can manage multiple loader at the same time, that is, Loadermanager and loader are one-to-many relationships. We use loader in activity or fragment, although loader has a lot of public methods, but we can't call these public methods of loader directly, because it disrupts Loadermanag's normal management of loader. We should indirectly manage loader through Loadermanager's Initloader and Restartloader methods, and through the Loadercallbacks callback method (Oncreateloader, Onloadfinished, Onloaderreset) to handle the data accordingly.

The loader itself does not have the ability to load data asynchronously, Asynctaskloader inherits from loader, the data can be loaded asynchronously via Asynctask, Cursorloader inherits from Asynctaskloader, Cursor data can be loaded asynchronously.

We use loader in activity or fragment, activity, fragment and loadermanagement interact like Client-server mode, That is, activity or fragment is the client side of the Client-server model, that is, clients, in this article, we refer to the client refers to the loader user, that is, activity or fragment.

in summary, loader has two major lifecycles: active and inactive, active and inactive. this is reflected in the source code of Loadermanager is very intuitive: Loadermanager inside there are two arrays mloaders and Minactiveloaders, Where Mloaders stores all loader,minactiveloaders in the active state and stores all loader in inactive state.

Subdivision, Loader's active state is divided into started state and stopped state, that is, the start state and the stop State, note that the stopped state is also the active life cycle. The inactive state of loader is divided into abandoned state and reseted state, that is, discard state and reset state, in which abandoned state indicates that loader is now obsolete and will be reset over time. The reseted state indicates that the loader is completely destroyed and reused. Loader active from high to low in order to started-stopped, abandoned-reseted, which also corresponds to the loader of four life cycle, loader in the started state of the most active, Loader is completely inactive while in the reseted state. In fact, the loader in the stopped state is also likely to change to started state again, so we will slightly change the one-way arrows above started to stopped to two-way arrows, i.e. loader accurate life cycle should be started <- > Stopped, abandoned, reseted.

The following is an analysis of the source call process for each life cycle of loader and its subclasses, and it needs to be explained that because Asyncloader inherits from loader, Cursorloader inherits from Asyncloader, so when we parse the code, these three classes are involved, Because subclasses can implement or override methods of the parent class, such as the loader class itself has an empty method onstartloading (), Asyncloader does not override the method, However, Cursorloader overrides the empty method in Loader onstartloading () to cursorloader implement its own logic in the method.

It is important to note that Loadermanager is an abstract class, Android has a class Loadermanagerimpl implemented loadermanager,activity or fragment through Getloadermanager () The method returned is actually a Loadermanagerimpl instance, so we have to study Loadermanager and Loadermanagerimpl at the same time.

The source address of loader and its related classes is as follows:
Loader source
Asynctaskloader Source
Cursorloader Source
Loadermanager Source
Loadermanagerimpl Source

Started

Loader after executing the startloading () method, it will enter the started state, Loadermanager will execute the loader.startloading () method at the appropriate time, to summarize, When we execute Loadmanager.initloader (), if loader does not exist, internal execution Loadcallbacks Oncreateloader () method creates loader, And let the newly created loader execute the startloading () method.

The specific implementation process is as follows:
Loadermanager.initloader ()
Loadermanager.createandinstallloader ()
Loadermanager.createloader ()
Loadercallbacks.oncreateloader ()
Create Loaderinfo after getting loader
Loadermanager.installloader ()
Put it into the Loadermanager internally maintained Mloaders array,
Loaderinfo.start ()
Loader in started state
Loader.startloading ()

we can write our logic in onstartloading (), in started State, loader should monitor the change of the data source and send the new data to the client, specifically, when the new data is monitored, Invokes the Loader.deliverresult () method, triggering execution of the loadcallbacks.onloadfinished () callback, so that the client can easily obtain data from the callback .

Cursorloader overrides the Onstartloading () method in loader, the following is the logic behind Cursorloader execution to onstartloading (), From this we can see how Asyncloader and Cursorloader implement asynchronous loading data.

Cursorloader.onstartloading ()
Asynctaskloader.forceload ()
Loader.forceload ()
Asynctaskloader.onforceload ()
Asynctaskloader.executependingtask ()
Enter asynchronous Thread,
Loadtask.doinbackground ()
Asynctaskloader.onloadinbackground ()
Asynctaskloader.doinbackground ()
Get the actual data,
Exit Async thread, return to main thread,
Loadtask.onpostexecute ()
Asynctaskloader.dispatchonloadcomplete ()
Cursorloader.deliverresult (), Loader.deliverresult ()
Loaderinfo.onloadcomplete ()
Loaderinfo.callonloadfinished ()
Loadercallbacks.onloadfinished ()

Stopped

Loader after executing the stoploading () state, it will enter the stopped state, Loadermanager will execute the loader.stoploading () method at the appropriate time, in summary, The activity's Performstop () method is executed when the activity is about to be in the stopped state, and Fragmentcontroller's Doloaderstop () method is executed within the method. In this method, the Fragmenthostcallback Doloaderstop () method is executed, and the Loadermanager Dostop () method is executed inside the method, within which Loadermanager iterates through all the internal stored loaderinfo, executes Loaderinfo's Stop () method in turn, and calls the Loader.stoploading () method inside the method. At this point loader enters the stopped method and executes the loader stoploading ().

The specific implementation process for the started-stopped status changes is as follows:
Activity.performstop ()
Fragmentcontroller.doloaderstop ()
Fragmenthostcallback.doloaderstop ()
Loadermanager.dostop ()
All Loaderinfo.stop ()
Loader.stoploading ()
Loader Enter stopped status
Onstoploading (), we can write our logic in onstartloading ().

By the above call process we can find that when the activity is in the stopped state, all the loader will also be placed stopped state, in fact, if you go to observe fragment source code will also find that when fragment in stopped state, All loader will also be placed in the stopped state.

When activity executes the OnStart () method, it executes the Fragmentcontroller Doloaderstart () method internally, and then executes the Fragmenthostcallback Doloaderstart () method, which then executes the Dostart () method of the Loadermanager, which iterates through all loaderinfo and executes the Loaderinfo start () method, and then executes the loader () method, Thus loader enters the started state again, and then executes Loader's onstartloading ().

The specific implementation process for the stopped-started status changes is as follows:
Activity.onstart ()
Fragmentcontroller.doloaderstart ()
Fragmenthostcallback.doloaderstart ()
Loadermanager.dostart ()
All Loaderinfo.start ()
Loader.startloading ()
Loader from stopped state into started state
Loader.onstartloading ().

By the above call procedure we can find that when the activity is re-started from stopped, all loader in stopped state will be changed from stopped state to started state. In fact, if you look at the source of fragment will also find that when fragment from stopped state to started state, loader will also change from stopped state to started state.

From the above description, we can learn that Android wants to keep loader in line with its users (activity or fragment), with the same started or stopped.

When the loader is in the stopped state, we should continue to listen to the changes in the data, but when there is new data we should save it, do not send it to the client, because the client (activity or fragment) is also in the stopped state, It's no use sending it to the client. If the loader from the stopped state to the started state, then we should send the new data previously heard to the client (activity or fragment), because the client must also be in the started state at this time, after being sent to the client, The client can update the UI and so on.

Abandoned

Loader after executing the abandon () method, it will enter the abandoned state, Loadermanager will execute the Loader.abandon () method at the appropriate time, to summarize, When executing Loadermanager.restartloader (Loaderid), if the specified loaderid corresponding loader exists and is in the active state (specifically started and stopped two states), Then the original loader's abandon () method is called, so that the original loader is in the abandoned state, And Loadermanager will put the loader into the minactiveloaders array Loadermanager internally maintained, and we will create a new loader after the original abandoned enters the loader state. At this point, just put into the minactiveloaders array of loader into an old obsolete loader, notice at this time the old loader will not be destroyed, because we have created a new loader, but the new loader has not been able to obtain data, So at this point we still have to save the old loader, so that the client can not get the new loader data sent to continue to use the old loader data, once the new loader loaded the data and sent to the client, then the old loader at this time completely useless, Loadermanager will place it in the reseted state and remove it from the minactiveloaders, that is, the old loader is destroyed, and the details about how to change from abandoned to reseted are explained in detail from the code call.

The abandoned state is an intermediate state of loader from stopped to reseted state, and in general, abandoned in loader state is completely destroyed for a period of time and marked as reseted State, So the loader of the abandoned state should not listen to changes in the data source, and should not send the changed data source to the client.

reseted

After the reset () method is executed, the loader enters the rested state, and Loadermanager executes the Loader.reset () method at the appropriate time, as follows:
As mentioned above, after the Loadermanager Restartloader () method is executed, we put the original loader into the minactiveloaders and put it on the abandoned turntable, and a new loader is immediately created.

The new loader will begin its own new journey, with the following steps:
Loadmanager.initloader ()
Loadermanager.createandinstallloader
Loadcallbacks.oncreateloader ()
Loadermanager.installloader
Put it into the Loadermanager internally maintained Mloaders array,
Loader.startloading ()
Loader Enter started status
Loader.onstartloading (), we will load the data in Loader's onstartloading (), and when the load is complete, we will execute Loader.deliverresult (data) To send the data to the client, starting with this method, the sequence of calls is Loader.deliverresult (data),
Loaderinfo.onloadcomplete ()
Loaderinfo.callonloadfinished
Loadercallbacks.onloadfinished
The client gets the data.
Check the Loaderid in Minactiveloaders there is no corresponding Loaderid loader, if can find, Then it means that the abandoned loader from Minactiveloaders is precisely the time we explained abandoned because Loadermanager.restartloader (Loaderid) Execution is plugged into minactiveloaders and is in abandoned state, then this old loader in minactiveloaders is completely useless, Loadermanager will call the discarded loader reset () method, the specific execution process for the deprecated loader is:
Abandoned Loaderinfo.destroy ()
Abandoned Loader.reset ()
Abandoned Loader.onreset ()
Obsolete loader This state changes from abandoned to rested state
Finally Loadermanager removed it from the minactiveloaders, i.e. the old loader was destroyed.

The loader in the reseted state is completely destroyed, or loadermanager the loader marked as the destruction state reseted.

I hope this article to understand the life cycle of loader help, in understanding the loader life cycle, we can write to meet their own needs of the loader, the following will write a custom loader article.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you think the article is good, remember the top! If there is a doubt that the article is wrong, welcome to comment on my message or correct, see will be timely reply.

Deep source parsing Android loader, Asynctaskloader, Cursorloader, Loadermanager

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.