blob: 4b406116ff6c89b2c699b006c294607ba60176da [file] [log] [blame]
Joe Malin50c22752012-10-20 12:41:40 -07001page.title=Setting Up the Loader
2trainingnavtop=true
3startpage=true
4
5@jd:body
6
7<!-- This is the training bar -->
8<div id="tb-wrapper">
9 <div id="tb">
10<h2>This lesson teaches you to</h2>
11<ol>
12 <li>
13 <a href="#AddExtensions">Extend an Activity</a>
14 </li>
15 <li>
16 <a href="#GetLoader">Retrieve a LoaderManager</a>
17 </li>
18 <li>
19 <a href="#InitializeLoader">Initialize the Loader Framework</a>
20 </li>
21</ol>
22 </div>
23</div>
24<p>
25 You create a {@link android.support.v4.content.CursorLoader} within a
26 <b>loader framework</b>. To set up the framework, you implement the
27 {@link android.support.v4.app.LoaderManager.LoaderCallbacks LoaderCallbacks&lt;Cursor&gt;}
28 as part of an {@link android.app.Activity}. In addition, to provide compatibility
29 compatible with platform versions starting with Android 1.6, you must extend the
30 {@link android.app.Activity} with the {@link android.support.v4.app.FragmentActivity} class.
31</p>
32<p class="note">
33 <strong>Note:</strong> A {@link android.support.v4.app.Fragment} is not a prerequisite for
34 {@link android.support.v4.content.CursorLoader}. As a convenience, the support library class
35 {@link android.support.v4.app.FragmentActivity} contains the fragment and the loader frameworks,
36 but they are completely independent of each other.
37</p>
38<p>
39 Before you can use the loader framework, you need to initialize it. To do this, retrieve
40 a {@link android.support.v4.app.LoaderManager} object and call its
41 {@link android.support.v4.app.LoaderManager#initLoader initLoader()} method.
42</p>
43<p>
44 If you do use one or more {@link android.support.v4.app.Fragment} objects in an
45 {@link android.app.Activity}, the {@link android.support.v4.app.LoaderManager} you retrieve is
46 available to all of them.
47</p>
48<h2 id="AddExtensions">Extend an Activity</h2>
49<p>
50 To set up an {@link android.app.Activity} subclass to contain a
51 {@link android.support.v4.content.CursorLoader}, extend the subclass with
52 must extend {@link android.support.v4.app.FragmentActivity}, which provides the loader
53 framework, and implement the {@link android.support.v4.app.LoaderManager.LoaderCallbacks
54 LoaderCallbacks&lt;Cursor&gt;} interface, which specifies method signatures that the loader
55 framework uses to interact with the {@link android.app.Activity}.
56</p>
57<p>
58 For example:
59</p>
60<pre>
61public class DisplayActivity extends FragmentActivity
62 implements LoaderManager.LoaderCallbacks&lt;Cursor&gt;
63</pre>
64<h2 id="GetLoader">Retrieve a LoaderManager</h2>
65<p>
66 To get an instance {@link android.support.v4.app.LoaderManager} for use in your
67 {@link android.app.Activity}, call
68 {@link android.support.v4.app.FragmentActivity#getSupportLoaderManager
69 FragmentActivity.getSupportLoaderManager()} at the beginning of the
70 {@link android.app.Activity#onCreate onCreate()} method. For example:
71</p>
72<pre>
73private LoaderManager mLoaderManager;
74public void onCreate() {
75...
76mLoaderManager = this.getSupportLoaderManager();
77</pre>
78<h2 id="InitializeLoader">Initialize the Loader Framework</h2>
79<p>
80 Once you have the {@link android.support.v4.app.LoaderManager} object, initialize
81 it by calling {@link android.support.v4.app.LoaderManager#initLoader initLoader()}. For
82 example:
83</p>
84<pre>
85// CursorLoader instance identifier
86public static final int URL_LOADER = 0;
87...
88// Initializes the CursorLoader
89getSupportLoaderManager().initLoader(URL_LOADER, null, this);
90</pre>