FragmentActivity.java revision 346e2f2390f0d743fd10e7d01a015df6b32292cd
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.app;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.Configuration;
23import android.content.res.TypedArray;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.Message;
27import android.os.Parcelable;
28import android.support.v4.util.SparseArrayCompat;
29import android.util.AttributeSet;
30import android.util.Log;
31import android.view.KeyEvent;
32import android.view.Menu;
33import android.view.MenuItem;
34import android.view.View;
35import android.view.Window;
36
37import java.io.FileDescriptor;
38import java.io.PrintWriter;
39import java.util.ArrayList;
40import java.util.HashMap;
41
42/**
43 * Base class for activities that want to use the support-based
44 * {@link android.support.v4.app.Fragment} and
45 * {@link android.support.v4.content.Loader} APIs.
46 *
47 * <p>When using this class as opposed to new platform's built-in fragment
48 * and loader support, you must use the {@link #getSupportFragmentManager()}
49 * and {@link #getSupportLoaderManager()} methods respectively to access
50 * those features.
51 *
52 * <p>Known limitations:</p>
53 * <ul>
54 * <li> <p>When using the &lt;fragment> tag, this implementation can not
55 * use the parent view's ID as the new fragment's ID.  You must explicitly
56 * specify an ID (or tag) in the &lt;fragment>.</p>
57 * <li> <p>Prior to Honeycomb (3.0), an activity's state was saved before pausing.
58 * Fragments are a significant amount of new state, and dynamic enough that one
59 * often wants them to change between pausing and stopping.  These classes
60 * throw an exception if you try to change the fragment state after it has been
61 * saved, to avoid accidental loss of UI state.  However this is too restrictive
62 * prior to Honeycomb, where the state is saved before pausing.  To address this,
63 * when running on platforms prior to Honeycomb an exception will not be thrown
64 * if you change fragments between the state save and the activity being stopped.
65 * This means that in some cases if the activity is restored from its last saved
66 * state, this may be a snapshot slightly before what the user last saw.</p>
67 * </ul>
68 */
69public class FragmentActivity extends Activity {
70    private static final String TAG = "FragmentActivity";
71
72    private static final String FRAGMENTS_TAG = "android:support:fragments";
73
74    // This is the SDK API version of Honeycomb (3.0).
75    private static final int HONEYCOMB = 11;
76
77    static final int MSG_REALLY_STOPPED = 1;
78    static final int MSG_RESUME_PENDING = 2;
79
80    final Handler mHandler = new Handler() {
81        @Override
82        public void handleMessage(Message msg) {
83            switch (msg.what) {
84                case MSG_REALLY_STOPPED:
85                    if (mStopped) {
86                        doReallyStop(false);
87                    }
88                    break;
89                case MSG_RESUME_PENDING:
90                    onResumeFragments();
91                    mFragments.execPendingActions();
92                    break;
93                default:
94                    super.handleMessage(msg);
95            }
96        }
97
98    };
99    final FragmentManagerImpl mFragments = new FragmentManagerImpl();
100
101    boolean mCreated;
102    boolean mResumed;
103    boolean mStopped;
104    boolean mReallyStopped;
105    boolean mRetaining;
106
107    boolean mOptionsMenuInvalidated;
108
109    boolean mCheckedForLoaderManager;
110    boolean mLoadersStarted;
111    SparseArrayCompat<LoaderManagerImpl> mAllLoaderManagers;
112    LoaderManagerImpl mLoaderManager;
113
114    static final class NonConfigurationInstances {
115        Object activity;
116        Object custom;
117        HashMap<String, Object> children;
118        ArrayList<Fragment> fragments;
119        SparseArrayCompat<LoaderManagerImpl> loaders;
120    }
121
122    static class FragmentTag {
123        public static final int[] Fragment = {
124            0x01010003, 0x010100d0, 0x010100d1
125        };
126        public static final int Fragment_id = 1;
127        public static final int Fragment_name = 0;
128        public static final int Fragment_tag = 2;
129    }
130
131    // ------------------------------------------------------------------------
132    // HOOKS INTO ACTIVITY
133    // ------------------------------------------------------------------------
134
135    /**
136     * Dispatch incoming result to the correct fragment.
137     */
138    @Override
139    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
140        int index = requestCode>>16;
141        if (index != 0) {
142            index--;
143            if (mFragments.mActive == null || index < 0 || index >= mFragments.mActive.size()) {
144                Log.w(TAG, "Activity result fragment index out of range: 0x"
145                        + Integer.toHexString(requestCode));
146                return;
147            }
148            Fragment frag = mFragments.mActive.get(index);
149            if (frag == null) {
150                Log.w(TAG, "Activity result no fragment exists for index: 0x"
151                        + Integer.toHexString(requestCode));
152            }
153            frag.onActivityResult(requestCode&0xffff, resultCode, data);
154            return;
155        }
156
157        super.onActivityResult(requestCode, resultCode, data);
158    }
159
160    /**
161     * Take care of popping the fragment back stack or finishing the activity
162     * as appropriate.
163     */
164    public void onBackPressed() {
165        if (!mFragments.popBackStackImmediate()) {
166            finish();
167        }
168    }
169
170    /**
171     * Dispatch configuration change to all fragments.
172     */
173    @Override
174    public void onConfigurationChanged(Configuration newConfig) {
175        super.onConfigurationChanged(newConfig);
176        mFragments.dispatchConfigurationChanged(newConfig);
177    }
178
179    /**
180     * Perform initialization of all fragments and loaders.
181     */
182    @Override
183    protected void onCreate(Bundle savedInstanceState) {
184        mFragments.attachActivity(this);
185        // Old versions of the platform didn't do this!
186        if (getLayoutInflater().getFactory() == null) {
187            getLayoutInflater().setFactory(this);
188        }
189
190        super.onCreate(savedInstanceState);
191
192        NonConfigurationInstances nc = (NonConfigurationInstances)
193                getLastNonConfigurationInstance();
194        if (nc != null) {
195            mAllLoaderManagers = nc.loaders;
196        }
197        if (savedInstanceState != null) {
198            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
199            mFragments.restoreAllState(p, nc != null ? nc.fragments : null);
200        }
201        mFragments.dispatchCreate();
202    }
203
204    /**
205     * Dispatch to Fragment.onCreateOptionsMenu().
206     */
207    @Override
208    public boolean onCreatePanelMenu(int featureId, Menu menu) {
209        if (featureId == Window.FEATURE_OPTIONS_PANEL) {
210            boolean show = super.onCreatePanelMenu(featureId, menu);
211            show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
212            if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
213                return show;
214            }
215            // Prior to Honeycomb, the framework can't invalidate the options
216            // menu, so we must always say we have one in case the app later
217            // invalidates it and needs to have it shown.
218            return true;
219        }
220        return super.onCreatePanelMenu(featureId, menu);
221    }
222
223    /**
224     * Add support for inflating the &lt;fragment> tag.
225     */
226    @Override
227    public View onCreateView(String name, Context context, AttributeSet attrs) {
228        if (!"fragment".equals(name)) {
229            return super.onCreateView(name, context, attrs);
230        }
231
232        String fname = attrs.getAttributeValue(null, "class");
233        TypedArray a =  context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
234        if (fname == null) {
235            fname = a.getString(FragmentTag.Fragment_name);
236        }
237        int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
238        String tag = a.getString(FragmentTag.Fragment_tag);
239        a.recycle();
240
241        View parent = null; // NOTE: no way to get parent pre-Honeycomb.
242        int containerId = parent != null ? parent.getId() : 0;
243        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
244            throw new IllegalArgumentException(attrs.getPositionDescription()
245                    + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
246        }
247
248        // If we restored from a previous state, we may already have
249        // instantiated this fragment from the state and should use
250        // that instance instead of making a new one.
251        Fragment fragment = id != View.NO_ID ? mFragments.findFragmentById(id) : null;
252        if (fragment == null && tag != null) {
253            fragment = mFragments.findFragmentByTag(tag);
254        }
255        if (fragment == null && containerId != View.NO_ID) {
256            fragment = mFragments.findFragmentById(containerId);
257        }
258
259        if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
260                + Integer.toHexString(id) + " fname=" + fname
261                + " existing=" + fragment);
262        if (fragment == null) {
263            fragment = Fragment.instantiate(this, fname);
264            fragment.mFromLayout = true;
265            fragment.mFragmentId = id != 0 ? id : containerId;
266            fragment.mContainerId = containerId;
267            fragment.mTag = tag;
268            fragment.mInLayout = true;
269            fragment.mFragmentManager = mFragments;
270            fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
271            mFragments.addFragment(fragment, true);
272
273        } else if (fragment.mInLayout) {
274            // A fragment already exists and it is not one we restored from
275            // previous state.
276            throw new IllegalArgumentException(attrs.getPositionDescription()
277                    + ": Duplicate id 0x" + Integer.toHexString(id)
278                    + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
279                    + " with another fragment for " + fname);
280        } else {
281            // This fragment was retained from a previous instance; get it
282            // going now.
283            fragment.mInLayout = true;
284            // If this fragment is newly instantiated (either right now, or
285            // from last saved state), then give it the attributes to
286            // initialize itself.
287            if (!fragment.mRetaining) {
288                fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
289            }
290            mFragments.moveToState(fragment);
291        }
292
293        if (fragment.mView == null) {
294            throw new IllegalStateException("Fragment " + fname
295                    + " did not create a view.");
296        }
297        if (id != 0) {
298            fragment.mView.setId(id);
299        }
300        if (fragment.mView.getTag() == null) {
301            fragment.mView.setTag(tag);
302        }
303        return fragment.mView;
304    }
305
306    /**
307     * Destroy all fragments and loaders.
308     */
309    @Override
310    protected void onDestroy() {
311        super.onDestroy();
312
313        doReallyStop(false);
314
315        mFragments.dispatchDestroy();
316        if (mLoaderManager != null) {
317            mLoaderManager.doDestroy();
318        }
319    }
320
321    /**
322     * Take care of calling onBackPressed() for pre-Eclair platforms.
323     */
324    @Override
325    public boolean onKeyDown(int keyCode, KeyEvent event) {
326        if (android.os.Build.VERSION.SDK_INT < 5 /* ECLAIR */
327                && keyCode == KeyEvent.KEYCODE_BACK
328                && event.getRepeatCount() == 0) {
329            // Take care of calling this method on earlier versions of
330            // the platform where it doesn't exist.
331            onBackPressed();
332            return true;
333        }
334
335        return super.onKeyDown(keyCode, event);
336    }
337
338    /**
339     * Dispatch onLowMemory() to all fragments.
340     */
341    @Override
342    public void onLowMemory() {
343        super.onLowMemory();
344        mFragments.dispatchLowMemory();
345    }
346
347    /**
348     * Dispatch context and options menu to fragments.
349     */
350    @Override
351    public boolean onMenuItemSelected(int featureId, MenuItem item) {
352        if (super.onMenuItemSelected(featureId, item)) {
353            return true;
354        }
355
356        switch (featureId) {
357            case Window.FEATURE_OPTIONS_PANEL:
358                return mFragments.dispatchOptionsItemSelected(item);
359
360            case Window.FEATURE_CONTEXT_MENU:
361                return mFragments.dispatchContextItemSelected(item);
362
363            default:
364                return false;
365        }
366    }
367
368    /**
369     * Call onOptionsMenuClosed() on fragments.
370     */
371    @Override
372    public void onPanelClosed(int featureId, Menu menu) {
373        switch (featureId) {
374            case Window.FEATURE_OPTIONS_PANEL:
375                mFragments.dispatchOptionsMenuClosed(menu);
376                break;
377        }
378        super.onPanelClosed(featureId, menu);
379    }
380
381    /**
382     * Dispatch onPause() to fragments.
383     */
384    @Override
385    protected void onPause() {
386        super.onPause();
387        mResumed = false;
388        if (mHandler.hasMessages(MSG_RESUME_PENDING)) {
389            mHandler.removeMessages(MSG_RESUME_PENDING);
390            onResumeFragments();
391        }
392        mFragments.dispatchPause();
393    }
394
395    /**
396     * Dispatch onResume() to fragments.  Note that for better inter-operation
397     * with older versions of the platform, at the point of this call the
398     * fragments attached to the activity are <em>not</em> resumed.  This means
399     * that in some cases the previous state may still be saved, not allowing
400     * fragment transactions that modify the state.  To correctly interact
401     * with fragments in their proper state, you should instead override
402     * {@link #onResumeFragments()}.
403     */
404    @Override
405    protected void onResume() {
406        super.onResume();
407        mHandler.sendEmptyMessage(MSG_RESUME_PENDING);
408        mResumed = true;
409        mFragments.execPendingActions();
410    }
411
412    /**
413     * Dispatch onResume() to fragments.
414     */
415    @Override
416    protected void onPostResume() {
417        super.onPostResume();
418        mHandler.removeMessages(MSG_RESUME_PENDING);
419        onResumeFragments();
420        mFragments.execPendingActions();
421    }
422
423    /**
424     * This is the fragment-orientated version of {@link #onResume()} that you
425     * can override to perform operations in the Activity at the same point
426     * where its fragments are resumed.  Be sure to always call through to
427     * the super-class.
428     */
429    protected void onResumeFragments() {
430        mFragments.dispatchResume();
431    }
432
433    /**
434     * Dispatch onPrepareOptionsMenu() to fragments.
435     */
436    @Override
437    public boolean onPreparePanel(int featureId, View view, Menu menu) {
438        if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
439            if (mOptionsMenuInvalidated) {
440                mOptionsMenuInvalidated = false;
441                menu.clear();
442                onCreatePanelMenu(featureId, menu);
443            }
444            boolean goforit = super.onPreparePanel(featureId, view, menu);
445            goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
446            return goforit && menu.hasVisibleItems();
447        }
448        return super.onPreparePanel(featureId, view, menu);
449    }
450
451    /**
452     * Retain all appropriate fragment and loader state.  You can NOT
453     * override this yourself!  Use {@link #onRetainCustomNonConfigurationInstance()}
454     * if you want to retain your own state.
455     */
456    @Override
457    public final Object onRetainNonConfigurationInstance() {
458        if (mStopped) {
459            doReallyStop(true);
460        }
461
462        Object custom = onRetainCustomNonConfigurationInstance();
463
464        ArrayList<Fragment> fragments = mFragments.retainNonConfig();
465        boolean retainLoaders = false;
466        if (mAllLoaderManagers != null) {
467            // prune out any loader managers that were already stopped and so
468            // have nothing useful to retain.
469            for (int i=mAllLoaderManagers.size()-1; i>=0; i--) {
470                LoaderManagerImpl lm = mAllLoaderManagers.valueAt(i);
471                if (lm.mRetaining) {
472                    retainLoaders = true;
473                } else {
474                    lm.doDestroy();
475                    mAllLoaderManagers.removeAt(i);
476                }
477            }
478        }
479        if (fragments == null && !retainLoaders && custom == null) {
480            return null;
481        }
482
483        NonConfigurationInstances nci = new NonConfigurationInstances();
484        nci.activity = null;
485        nci.custom = custom;
486        nci.children = null;
487        nci.fragments = fragments;
488        nci.loaders = mAllLoaderManagers;
489        return nci;
490    }
491
492    /**
493     * Save all appropriate fragment state.
494     */
495    @Override
496    protected void onSaveInstanceState(Bundle outState) {
497        super.onSaveInstanceState(outState);
498        Parcelable p = mFragments.saveAllState();
499        if (p != null) {
500            outState.putParcelable(FRAGMENTS_TAG, p);
501        }
502    }
503
504    /**
505     * Dispatch onStart() to all fragments.  Ensure any created loaders are
506     * now started.
507     */
508    @Override
509    protected void onStart() {
510        super.onStart();
511
512        mStopped = false;
513        mReallyStopped = false;
514        mHandler.removeMessages(MSG_REALLY_STOPPED);
515
516        if (!mCreated) {
517            mCreated = true;
518            mFragments.dispatchActivityCreated();
519        }
520
521        mFragments.noteStateNotSaved();
522        mFragments.execPendingActions();
523
524        if (!mLoadersStarted) {
525            mLoadersStarted = true;
526            if (mLoaderManager != null) {
527                mLoaderManager.doStart();
528            } else if (!mCheckedForLoaderManager) {
529                mLoaderManager = getLoaderManager(-1, mLoadersStarted, false);
530            }
531            mCheckedForLoaderManager = true;
532        }
533        // NOTE: HC onStart goes here.
534
535        mFragments.dispatchStart();
536        if (mAllLoaderManagers != null) {
537            for (int i=mAllLoaderManagers.size()-1; i>=0; i--) {
538                LoaderManagerImpl lm = mAllLoaderManagers.valueAt(i);
539                lm.finishRetain();
540                lm.doReportStart();
541            }
542        }
543    }
544
545    /**
546     * Dispatch onStop() to all fragments.  Ensure all loaders are stopped.
547     */
548    @Override
549    protected void onStop() {
550        super.onStop();
551
552        mStopped = true;
553        mHandler.sendEmptyMessage(MSG_REALLY_STOPPED);
554
555        mFragments.dispatchStop();
556    }
557
558    // ------------------------------------------------------------------------
559    // NEW METHODS
560    // ------------------------------------------------------------------------
561
562    /**
563     * Use this instead of {@link #onRetainNonConfigurationInstance()}.
564     * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}.
565     */
566    public Object onRetainCustomNonConfigurationInstance() {
567        return null;
568    }
569
570    /**
571     * Return the value previously returned from
572     * {@link #onRetainCustomNonConfigurationInstance()}.
573     */
574    public Object getLastCustomNonConfigurationInstance() {
575        NonConfigurationInstances nc = (NonConfigurationInstances)
576                getLastNonConfigurationInstance();
577        return nc != null ? nc.custom : null;
578    }
579
580    /**
581     * Invalidate the activity's options menu. This will cause relevant presentations
582     * of the menu to fully update via calls to onCreateOptionsMenu and
583     * onPrepareOptionsMenu the next time the menu is requested.
584     */
585    public void supportInvalidateOptionsMenu() {
586        if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
587            // If we are running on HC or greater, we can use the framework
588            // API to invalidate the options menu.
589            ActivityCompatHoneycomb.invalidateOptionsMenu(this);
590            return;
591        }
592
593        // Whoops, older platform...  we'll use a hack, to manually rebuild
594        // the options menu the next time it is prepared.
595        mOptionsMenuInvalidated = true;
596    }
597
598    /**
599     * Print the Activity's state into the given stream.  This gets invoked if
600     * you run "adb shell dumpsys activity <activity_component_name>".
601     *
602     * @param prefix Desired prefix to prepend at each line of output.
603     * @param fd The raw file descriptor that the dump is being sent to.
604     * @param writer The PrintWriter to which you should dump your state.  This will be
605     * closed for you after you return.
606     * @param args additional arguments to the dump request.
607     */
608    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
609        if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
610            // XXX This can only work if we can call the super-class impl. :/
611            //ActivityCompatHoneycomb.dump(this, prefix, fd, writer, args);
612        }
613        writer.print(prefix); writer.print("Local FragmentActivity ");
614                writer.print(Integer.toHexString(System.identityHashCode(this)));
615                writer.println(" State:");
616        String innerPrefix = prefix + "  ";
617        writer.print(innerPrefix); writer.print("mCreated=");
618                writer.print(mCreated); writer.print("mResumed=");
619                writer.print(mResumed); writer.print(" mStopped=");
620                writer.print(mStopped); writer.print(" mReallyStopped=");
621                writer.println(mReallyStopped);
622        writer.print(innerPrefix); writer.print("mLoadersStarted=");
623                writer.println(mLoadersStarted);
624        if (mLoaderManager != null) {
625            writer.print(prefix); writer.print("Loader Manager ");
626                    writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager)));
627                    writer.println(":");
628            mLoaderManager.dump(prefix + "  ", fd, writer, args);
629        }
630        mFragments.dump(prefix, fd, writer, args);
631    }
632
633    void doReallyStop(boolean retaining) {
634        if (!mReallyStopped) {
635            mReallyStopped = true;
636            mRetaining = retaining;
637            mHandler.removeMessages(MSG_REALLY_STOPPED);
638            onReallyStop();
639        }
640    }
641
642    /**
643     * Pre-HC, we didn't have a way to determine whether an activity was
644     * being stopped for a config change or not until we saw
645     * onRetainNonConfigurationInstance() called after onStop().  However
646     * we need to know this, to know whether to retain fragments.  This will
647     * tell us what we need to know.
648     */
649    void onReallyStop() {
650        if (mLoadersStarted) {
651            mLoadersStarted = false;
652            if (mLoaderManager != null) {
653                if (!mRetaining) {
654                    mLoaderManager.doStop();
655                } else {
656                    mLoaderManager.doRetain();
657                }
658            }
659        }
660
661        mFragments.dispatchReallyStop();
662    }
663
664    // ------------------------------------------------------------------------
665    // FRAGMENT SUPPORT
666    // ------------------------------------------------------------------------
667
668    /**
669     * Called when a fragment is attached to the activity.
670     */
671    public void onAttachFragment(Fragment fragment) {
672    }
673
674    /**
675     * Return the FragmentManager for interacting with fragments associated
676     * with this activity.
677     */
678    public FragmentManager getSupportFragmentManager() {
679        return mFragments;
680    }
681
682    /**
683     * Modifies the standard behavior to allow results to be delivered to fragments.
684     * This imposes a restriction that requestCode be <= 0xffff.
685     */
686    @Override
687    public void startActivityForResult(Intent intent, int requestCode) {
688        if (requestCode != -1 && (requestCode&0xffff0000) != 0) {
689            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
690        }
691        super.startActivityForResult(intent, requestCode);
692    }
693
694    /**
695     * Called by Fragment.startActivityForResult() to implement its behavior.
696     */
697    public void startActivityFromFragment(Fragment fragment, Intent intent,
698            int requestCode) {
699        if (requestCode == -1) {
700            super.startActivityForResult(intent, -1);
701            return;
702        }
703        if ((requestCode&0xffff0000) != 0) {
704            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
705        }
706        super.startActivityForResult(intent, ((fragment.mIndex+1)<<16) + (requestCode&0xffff));
707    }
708
709    void invalidateSupportFragmentIndex(int index) {
710        //Log.v(TAG, "invalidateFragmentIndex: index=" + index);
711        if (mAllLoaderManagers != null) {
712            LoaderManagerImpl lm = mAllLoaderManagers.get(index);
713            if (lm != null && !lm.mRetaining) {
714                lm.doDestroy();
715                mAllLoaderManagers.remove(index);
716            }
717        }
718    }
719
720    // ------------------------------------------------------------------------
721    // LOADER SUPPORT
722    // ------------------------------------------------------------------------
723
724    /**
725     * Return the LoaderManager for this fragment, creating it if needed.
726     */
727    public LoaderManager getSupportLoaderManager() {
728        if (mLoaderManager != null) {
729            return mLoaderManager;
730        }
731        mCheckedForLoaderManager = true;
732        mLoaderManager = getLoaderManager(-1, mLoadersStarted, true);
733        return mLoaderManager;
734    }
735
736    LoaderManagerImpl getLoaderManager(int index, boolean started, boolean create) {
737        if (mAllLoaderManagers == null) {
738            mAllLoaderManagers = new SparseArrayCompat<LoaderManagerImpl>();
739        }
740        LoaderManagerImpl lm = mAllLoaderManagers.get(index);
741        if (lm == null) {
742            if (create) {
743                lm = new LoaderManagerImpl(this, started);
744                mAllLoaderManagers.put(index, lm);
745            }
746        } else {
747            lm.updateActivity(this);
748        }
749        return lm;
750    }
751}
752