loader

Download Loader

If you can't read please download the document

Upload: handaru-sakti

Post on 14-Jun-2015

425 views

Category:

Documents


1 download

DESCRIPTION

Asynchronously load data in an activity or fragment.

TRANSCRIPT

  • 1. Loader+Handaru Sakti

2. Intoduction Asynchronously load data in an activity orfragment. Automatically reconnect to the last their cursorwhen being recreated after a configurationchange (like screen flips). Thus, they dont needto re-query their data. Introduced in Honeycomb, exist on AndroidSupport Package v-4. 3. Involved Classes and Interfaces LoaderManager LoaderManager.LoaderCallbacks Loader AsyncTaskLoader CursorLoader 4. Common Approach... LoaderManager to initialize a Loader. There isonly one LoaderManager per activity orfragment, but a LoaderManager can havemultiple loaders. LoaderManager.LoaderCallbacks: a callbackinterface for a client to interact with theLoaderManager. Loader: an abstract class that performsasynchronous loading of data. This is the baseclass for a loader. 5. ...Common Approach AsyncTaskLoader: abstract loader that providesan AsyncTask to do the work. A CursorLoader used to load data whichbacked by a ContentProvider. Alternatively,implement a subclass of Loader orAsyncTaskLoader to load data from some othersource. A SimpleCursorAdapter used to display theloaders data. 6. Implementation Starting a Loader getLoaderManager().initLoader(0, null, this); Within Activity via onCreate() method. Within Fragment via onActivityCreated() method. Restarting a Loader getLoaderManager().restartLoader(0, null, this); 7. Using the LoaderManager CallbacksLoaderManager.LoaderCallbacks includes thesemethods: onCreateLoader() Instantiate and return a newLoader for the given ID. onLoadFinished() Called when a previouslycreated Loader has finished its load. onLoaderReset() Called when a previouslycreated Loader is being reset, thus making its data. 8. DEMO LoaderTest 9. References http://developer.android.com/guide/components/loaders.html http://developer.android.com/reference/android/content/Loader.html