The one of the advantage of loaders over asynctask is that they can handle configuration change ie rotation.But I am not able to understand how they do this and what exactly this means.Can anyone give a suitable example for this?.
Thanks in advance
The one of the advantage of loaders over asynctask is that they can handle configuration change ie rotation.But I am not able to understand how they do this and what exactly this means.Can anyone give a suitable example for this?.
Thanks in advance
Suppose you use a CursorLoader
to retrieve data from a ContentProvider
.
Then, suppose the user rotates the screen from portrait to landscape. Your portrait activity will be destroyed, and a new instance of your activity will be created so that you can display your landscape UI.
The CursorLoader
will not re-retrieve the data from the ContentProvider
. Rather, your new activity instance will be handed back the same Cursor
that had been delivered to your original activity instance.
Activity
(for API Level 11+) and FragmentActivity
(for API Level 10 and below) both will retain the CursorLoader
and its Cursor
across the configuration change. You can use a retained fragment (i.e., a fragment on which you call setRetainInstance(true)
) to also retain arbitrary objects across configuration changes.
Loaders are more useful than asyntask in many cases of screen orientation and all.Coding is easy in loaders
You can see a detailed explanation here