Joe Malin | ba34f09 | 2012-11-05 17:22:24 -0800 | [diff] [blame] | 1 | page.title=Loading Data in the Background |
Joe Fernandez | 33baa5a | 2013-11-14 11:41:19 -0800 | [diff] [blame] | 2 | page.tags=cursorloader |
Scott Main | 1c2dea0 | 2013-04-10 18:59:29 -0700 | [diff] [blame] | 3 | |
Joe Malin | ba34f09 | 2012-11-05 17:22:24 -0800 | [diff] [blame] | 4 | trainingnavtop=true |
| 5 | startpage=true |
| 6 | |
| 7 | @jd:body |
| 8 | <div id="tb-wrapper"> |
| 9 | <div id="tb"> |
| 10 | |
| 11 | <!-- Required platform, tools, add-ons, devices, knowledge, etc. --> |
| 12 | <h2>Dependencies and prerequisites</h2> |
| 13 | <ul> |
| 14 | <li> |
| 15 | Android 1.6 or later |
| 16 | </li> |
| 17 | </ul> |
| 18 | |
| 19 | <!-- related docs (NOT javadocs) --> |
| 20 | <h2>You should also read</h2> |
| 21 | <ul> |
| 22 | <li> |
| 23 | <a href="{@docRoot}guide/components/loaders.html">Loaders</a> |
| 24 | </li> |
| 25 | <li> |
| 26 | <a href="{@docRoot}guide/topics/data/data-storage.html#db">Using Databases</a> |
| 27 | </li> |
| 28 | <li> |
| 29 | <a href="{@docRoot}guide/topics/providers/content-provider-basics.html">Content Provider Basics</a> |
| 30 | </li> |
| 31 | </ul> |
| 32 | |
| 33 | <h2>Try it out</h2> |
| 34 | <div class="download-box"> |
| 35 | <a href="{@docRoot}shareables/training/ThreadSample.zip" class="button">Download the sample</a> |
| 36 | <p class="filename">ThreadSample.zip</p> |
| 37 | </div> |
| 38 | |
| 39 | </div> |
| 40 | </div> |
| 41 | <p> |
| 42 | Querying a {@link android.content.ContentProvider} for data you want to display takes time. |
| 43 | If you run the query directly from an {@link android.app.Activity}, it may get blocked and |
| 44 | cause the system to issue an "Application Not Responding" message. Even if it doesn't, users |
| 45 | will see an annoying delay in the UI. To avoid these problems, you should initiate a query on a |
| 46 | separate thread, wait for it to finish, and then display the results. |
| 47 | </p> |
| 48 | <p> |
| 49 | You can do this in a straightforward way by using an object that runs a query asynchronously in |
| 50 | the background and reconnects to your {@link android.app.Activity} when it's finished. This |
| 51 | object is a {@link android.support.v4.content.CursorLoader}. Besides doing the initial |
| 52 | background query, a {@link android.support.v4.content.CursorLoader} automatically re-runs the |
| 53 | query when data associated with the query changes. |
| 54 | </p> |
| 55 | <p> |
| 56 | This class describes how to use a {@link android.support.v4.content.CursorLoader} to run a |
Scott Main | 27c5755 | 2013-08-01 11:31:38 -0700 | [diff] [blame] | 57 | background query. Examples in this class use the <a |
| 58 | href="{@docRoot}tools/support-library/features.html#v4">v4 Support Library</a> |
Joe Malin | ba34f09 | 2012-11-05 17:22:24 -0800 | [diff] [blame] | 59 | versions of classes, which support platforms starting with Android 1.6. |
| 60 | </p> |
| 61 | <h2>Lessons</h2> |
| 62 | <dl> |
| 63 | <dt> |
| 64 | <strong><a href="setup-loader.html">Running a Query with a CursorLoader</a></strong> |
| 65 | </dt> |
| 66 | <dd> |
| 67 | Learn how to run a query in the background, using a |
| 68 | {@link android.support.v4.content.CursorLoader}. |
| 69 | </dd> |
| 70 | <dt> |
| 71 | <strong> |
| 72 | <a href="handle-results.html">Handling the Results</a> |
| 73 | </strong> |
| 74 | </dt> |
| 75 | <dd> |
| 76 | Learn how to handle the {@link android.database.Cursor} returned from the query, and how |
| 77 | to remove references to the current {@link android.database.Cursor} when the loader |
| 78 | framework re-sets the {@link android.support.v4.content.CursorLoader}. |
| 79 | </dd> |
| 80 | </dl> |