With 3.0 we got the fancy LoaderManager
, which handles data loading using the AsyncTaskLoader
, the CursorLoader
, and other custom Loader
instances. But reading through the docs for these I just couldn't get the point: how are these better than just using the good old AsyncTask
for data loading?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Well they are a lot simpler to implement, and take care of everything about lifecycle management so are much less error prone.
Just look at the sample code, for showing the result of a cursor query which lets the user interactively filter the result set through a query input field in the action bar:
Correctly implementing this full example yourself with AsyncTask is going to involve a lot more code... and even then, are you going to implement something as complete and well working? For example, will your implementation retain the loaded Cursor across activity configuration changes so it doesn't need to be re-queried when the new instances is created? LoaderManager/Loader will do that automatically for you, as well as taking care of correctly creating and closing the Cursor based on the activity lifecycle.
Also notice that using this code doesn't require that you think at all about making sure long running work is performed off the main UI thread. LoaderManager and CursorLoader take care of all of that for you, ensuring you will never block the main thread while interacting with the cursor. To do this correctly you actually need to have two Cursor objects active at the same time at points, so you can continue to display an interactive UI with your current Cursor while the next one to show is being loaded. LoaderManager does all of that for you.
This is just a much simpler API -- no need to know about AsyncTask and think about what needs to run in the background, no need to think about activity lifecycle or how to use the old "managed cursor" APIs in Activity (which didn't work as well as LoaderManager anyway).
(Btw don't forget the new "support" static library that let you use the full LoaderManager API on older versions of Android down to 1.6!)