FragmentActivity.java revision 7fe187efeb231d848f283ea9e4a0514ce2a8e862
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.Resources;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.Message;
27import android.os.Parcelable;
28import android.support.annotation.NonNull;
29import android.support.annotation.Nullable;
30import android.support.v4.util.SimpleArrayMap;
31import android.util.AttributeSet;
32import android.util.Log;
33import android.view.KeyEvent;
34import android.view.LayoutInflater;
35import android.view.Menu;
36import android.view.MenuItem;
37import android.view.View;
38import android.view.ViewGroup;
39import android.view.Window;
40
41import java.io.FileDescriptor;
42import java.io.PrintWriter;
43import java.util.ArrayList;
44import java.util.List;
45
46/**
47 * Base class for activities that want to use the support-based
48 * {@link android.support.v4.app.Fragment} and
49 * {@link android.support.v4.content.Loader} APIs.
50 *
51 * <p>When using this class as opposed to new platform's built-in fragment
52 * and loader support, you must use the {@link #getSupportFragmentManager()}
53 * and {@link #getSupportLoaderManager()} methods respectively to access
54 * those features.
55 *
56 * <p class="note"><strong>Note:</strong> If you want to implement an activity that includes
57 * an <a href="{@docRoot}guide/topics/ui/actionbar.html">action bar</a>, you should instead use
58 * the {@link android.support.v7.app.ActionBarActivity} class, which is a subclass of this one,
59 * so allows you to use {@link android.support.v4.app.Fragment} APIs on API level 7 and higher.</p>
60 *
61 * <p>Known limitations:</p>
62 * <ul>
63 * <li> <p>When using the <code>&lt;fragment></code> tag, this implementation can not
64 * use the parent view's ID as the new fragment's ID.  You must explicitly
65 * specify an ID (or tag) in the <code>&lt;fragment></code>.</p>
66 * <li> <p>Prior to Honeycomb (3.0), an activity's state was saved before pausing.
67 * Fragments are a significant amount of new state, and dynamic enough that one
68 * often wants them to change between pausing and stopping.  These classes
69 * throw an exception if you try to change the fragment state after it has been
70 * saved, to avoid accidental loss of UI state.  However this is too restrictive
71 * prior to Honeycomb, where the state is saved before pausing.  To address this,
72 * when running on platforms prior to Honeycomb an exception will not be thrown
73 * if you change fragments between the state save and the activity being stopped.
74 * This means that in some cases if the activity is restored from its last saved
75 * state, this may be a snapshot slightly before what the user last saw.</p>
76 * </ul>
77 */
78public class FragmentActivity extends Activity {
79    private static final String TAG = "FragmentActivity";
80
81    static final String FRAGMENTS_TAG = "android:support:fragments";
82
83    // This is the SDK API version of Honeycomb (3.0).
84    private static final int HONEYCOMB = 11;
85
86    static final int MSG_REALLY_STOPPED = 1;
87    static final int MSG_RESUME_PENDING = 2;
88
89    final Handler mHandler = new Handler() {
90        @Override
91        public void handleMessage(Message msg) {
92            switch (msg.what) {
93                case MSG_REALLY_STOPPED:
94                    if (mStopped) {
95                        doReallyStop(false);
96                    }
97                    break;
98                case MSG_RESUME_PENDING:
99                    onResumeFragments();
100                    mFragments.execPendingActions();
101                    break;
102                default:
103                    super.handleMessage(msg);
104            }
105        }
106
107    };
108    final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
109
110    boolean mCreated;
111    boolean mResumed;
112    boolean mStopped;
113    boolean mReallyStopped;
114    boolean mRetaining;
115
116    boolean mOptionsMenuInvalidated;
117
118    static final class NonConfigurationInstances {
119        Object custom;
120        ArrayList<Fragment> fragments;
121        SimpleArrayMap<String, LoaderManager> loaders;
122    }
123
124    // ------------------------------------------------------------------------
125    // HOOKS INTO ACTIVITY
126    // ------------------------------------------------------------------------
127
128    /**
129     * Dispatch incoming result to the correct fragment.
130     */
131    @Override
132    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
133        mFragments.noteStateNotSaved();
134        int index = requestCode>>16;
135        if (index != 0) {
136            index--;
137            final int activeFragmentsCount = mFragments.getActiveFragmentsCount();
138            if (activeFragmentsCount == 0 || index < 0 || index >= activeFragmentsCount) {
139                Log.w(TAG, "Activity result fragment index out of range: 0x"
140                        + Integer.toHexString(requestCode));
141                return;
142            }
143            final List<Fragment> activeFragments =
144                    mFragments.getActiveFragments(new ArrayList<Fragment>(activeFragmentsCount));
145            Fragment frag = activeFragments.get(index);
146            if (frag == null) {
147                Log.w(TAG, "Activity result no fragment exists for index: 0x"
148                        + Integer.toHexString(requestCode));
149            } else {
150                frag.onActivityResult(requestCode&0xffff, resultCode, data);
151            }
152            return;
153        }
154
155        super.onActivityResult(requestCode, resultCode, data);
156    }
157
158    /**
159     * Take care of popping the fragment back stack or finishing the activity
160     * as appropriate.
161     */
162    public void onBackPressed() {
163        if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
164            supportFinishAfterTransition();
165        }
166    }
167
168    /**
169     * Reverses the Activity Scene entry Transition and triggers the calling Activity
170     * to reverse its exit Transition. When the exit Transition completes,
171     * {@link #finish()} is called. If no entry Transition was used, finish() is called
172     * immediately and the Activity exit Transition is run.
173     *
174     * <p>On Android 4.4 or lower, this method only finishes the Activity with no
175     * special exit transition.</p>
176     */
177    public void supportFinishAfterTransition() {
178        ActivityCompat.finishAfterTransition(this);
179    }
180
181    /**
182     * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
183     * android.view.View, String)} was used to start an Activity, <var>callback</var>
184     * will be called to handle shared elements on the <i>launched</i> Activity. This requires
185     * {@link Window#FEATURE_CONTENT_TRANSITIONS}.
186     *
187     * @param callback Used to manipulate shared element transitions on the launched Activity.
188     */
189    public void setEnterSharedElementCallback(SharedElementCallback callback) {
190        ActivityCompat.setEnterSharedElementCallback(this, callback);
191    }
192
193    /**
194     * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
195     * android.view.View, String)} was used to start an Activity, <var>listener</var>
196     * will be called to handle shared elements on the <i>launching</i> Activity. Most
197     * calls will only come when returning from the started Activity.
198     * This requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
199     *
200     * @param listener Used to manipulate shared element transitions on the launching Activity.
201     */
202    public void setExitSharedElementCallback(SharedElementCallback listener) {
203        ActivityCompat.setExitSharedElementCallback(this, listener);
204    }
205
206    /**
207     * Support library version of {@link android.app.Activity#postponeEnterTransition()} that works
208     * only on API 21 and later.
209     */
210    public void supportPostponeEnterTransition() {
211        ActivityCompat.postponeEnterTransition(this);
212    }
213
214    /**
215     * Support library version of {@link android.app.Activity#startPostponedEnterTransition()}
216     * that only works with API 21 and later.
217     */
218    public void supportStartPostponedEnterTransition() {
219        ActivityCompat.startPostponedEnterTransition(this);
220    }
221
222    /**
223     * Dispatch configuration change to all fragments.
224     */
225    @Override
226    public void onConfigurationChanged(Configuration newConfig) {
227        super.onConfigurationChanged(newConfig);
228        mFragments.dispatchConfigurationChanged(newConfig);
229    }
230
231    /**
232     * Perform initialization of all fragments and loaders.
233     */
234    @SuppressWarnings("deprecation")
235    @Override
236    protected void onCreate(@Nullable Bundle savedInstanceState) {
237        mFragments.attachHost(null /*parent*/);
238        // Old versions of the platform didn't do this!
239        if (getLayoutInflater().getFactory() == null) {
240            getLayoutInflater().setFactory(this);
241        }
242
243        super.onCreate(savedInstanceState);
244
245        NonConfigurationInstances nc =
246                (NonConfigurationInstances) getLastNonConfigurationInstance();
247        if (nc != null) {
248            mFragments.restoreLoaderNonConfig(nc.loaders);
249        }
250        if (savedInstanceState != null) {
251            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
252            mFragments.restoreAllState(p, nc != null ? nc.fragments : null);
253        }
254        mFragments.dispatchCreate();
255    }
256
257    /**
258     * Dispatch to Fragment.onCreateOptionsMenu().
259     */
260    @Override
261    public boolean onCreatePanelMenu(int featureId, Menu menu) {
262        if (featureId == Window.FEATURE_OPTIONS_PANEL) {
263            boolean show = super.onCreatePanelMenu(featureId, menu);
264            show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
265            if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
266                return show;
267            }
268            // Prior to Honeycomb, the framework can't invalidate the options
269            // menu, so we must always say we have one in case the app later
270            // invalidates it and needs to have it shown.
271            return true;
272        }
273        return super.onCreatePanelMenu(featureId, menu);
274    }
275
276    /**
277     * Add support for inflating the &lt;fragment> tag.
278     */
279    @Override
280    @Nullable
281    public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) {
282        if (!"fragment".equals(name)) {
283            return super.onCreateView(name, context, attrs);
284        }
285
286        final View v = mFragments.onCreateView(null, name, context, attrs);
287        if (v == null) {
288            return super.onCreateView(name, context, attrs);
289        }
290        return v;
291    }
292
293    /**
294     * Destroy all fragments and loaders.
295     */
296    @Override
297    protected void onDestroy() {
298        super.onDestroy();
299
300        doReallyStop(false);
301
302        mFragments.dispatchDestroy();
303        mFragments.doLoaderDestroy();
304    }
305
306    /**
307     * Take care of calling onBackPressed() for pre-Eclair platforms.
308     */
309    @Override
310    public boolean onKeyDown(int keyCode, KeyEvent event) {
311        if (android.os.Build.VERSION.SDK_INT < 5 /* ECLAIR */
312                && keyCode == KeyEvent.KEYCODE_BACK
313                && event.getRepeatCount() == 0) {
314            // Take care of calling this method on earlier versions of
315            // the platform where it doesn't exist.
316            onBackPressed();
317            return true;
318        }
319
320        return super.onKeyDown(keyCode, event);
321    }
322
323    /**
324     * Dispatch onLowMemory() to all fragments.
325     */
326    @Override
327    public void onLowMemory() {
328        super.onLowMemory();
329        mFragments.dispatchLowMemory();
330    }
331
332    /**
333     * Dispatch context and options menu to fragments.
334     */
335    @Override
336    public boolean onMenuItemSelected(int featureId, MenuItem item) {
337        if (super.onMenuItemSelected(featureId, item)) {
338            return true;
339        }
340
341        switch (featureId) {
342            case Window.FEATURE_OPTIONS_PANEL:
343                return mFragments.dispatchOptionsItemSelected(item);
344
345            case Window.FEATURE_CONTEXT_MENU:
346                return mFragments.dispatchContextItemSelected(item);
347
348            default:
349                return false;
350        }
351    }
352
353    /**
354     * Call onOptionsMenuClosed() on fragments.
355     */
356    @Override
357    public void onPanelClosed(int featureId, Menu menu) {
358        switch (featureId) {
359            case Window.FEATURE_OPTIONS_PANEL:
360                mFragments.dispatchOptionsMenuClosed(menu);
361                break;
362        }
363        super.onPanelClosed(featureId, menu);
364    }
365
366    /**
367     * Dispatch onPause() to fragments.
368     */
369    @Override
370    protected void onPause() {
371        super.onPause();
372        mResumed = false;
373        if (mHandler.hasMessages(MSG_RESUME_PENDING)) {
374            mHandler.removeMessages(MSG_RESUME_PENDING);
375            onResumeFragments();
376        }
377        mFragments.dispatchPause();
378    }
379
380    /**
381     * Handle onNewIntent() to inform the fragment manager that the
382     * state is not saved.  If you are handling new intents and may be
383     * making changes to the fragment state, you want to be sure to call
384     * through to the super-class here first.  Otherwise, if your state
385     * is saved but the activity is not stopped, you could get an
386     * onNewIntent() call which happens before onResume() and trying to
387     * perform fragment operations at that point will throw IllegalStateException
388     * because the fragment manager thinks the state is still saved.
389     */
390    @Override
391    protected void onNewIntent(Intent intent) {
392        super.onNewIntent(intent);
393        mFragments.noteStateNotSaved();
394    }
395
396    /**
397     * Dispatch onResume() to fragments.  Note that for better inter-operation
398     * with older versions of the platform, at the point of this call the
399     * fragments attached to the activity are <em>not</em> resumed.  This means
400     * that in some cases the previous state may still be saved, not allowing
401     * fragment transactions that modify the state.  To correctly interact
402     * with fragments in their proper state, you should instead override
403     * {@link #onResumeFragments()}.
404     */
405    @Override
406    protected void onResume() {
407        super.onResume();
408        mHandler.sendEmptyMessage(MSG_RESUME_PENDING);
409        mResumed = true;
410        mFragments.execPendingActions();
411    }
412
413    /**
414     * Dispatch onResume() to fragments.
415     */
416    @Override
417    protected void onPostResume() {
418        super.onPostResume();
419        mHandler.removeMessages(MSG_RESUME_PENDING);
420        onResumeFragments();
421        mFragments.execPendingActions();
422    }
423
424    /**
425     * This is the fragment-orientated version of {@link #onResume()} that you
426     * can override to perform operations in the Activity at the same point
427     * where its fragments are resumed.  Be sure to always call through to
428     * the super-class.
429     */
430    protected void onResumeFragments() {
431        mFragments.dispatchResume();
432    }
433
434    /**
435     * Dispatch onPrepareOptionsMenu() to fragments.
436     */
437    @Override
438    public boolean onPreparePanel(int featureId, View view, Menu menu) {
439        if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
440            if (mOptionsMenuInvalidated) {
441                mOptionsMenuInvalidated = false;
442                menu.clear();
443                onCreatePanelMenu(featureId, menu);
444            }
445            boolean goforit = onPrepareOptionsPanel(view, menu);
446            goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
447            return goforit;
448        }
449        return super.onPreparePanel(featureId, view, menu);
450    }
451
452    /**
453     * @hide
454     */
455    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
456        return super.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, view, menu);
457    }
458
459    /**
460     * Retain all appropriate fragment and loader state.  You can NOT
461     * override this yourself!  Use {@link #onRetainCustomNonConfigurationInstance()}
462     * if you want to retain your own state.
463     */
464    @Override
465    public final Object onRetainNonConfigurationInstance() {
466        if (mStopped) {
467            doReallyStop(true);
468        }
469
470        Object custom = onRetainCustomNonConfigurationInstance();
471
472        ArrayList<Fragment> fragments = mFragments.retainNonConfig();
473        SimpleArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
474
475        if (fragments == null && loaders == null && custom == null) {
476            return null;
477        }
478
479        NonConfigurationInstances nci = new NonConfigurationInstances();
480        nci.custom = custom;
481        nci.fragments = fragments;
482        nci.loaders = loaders;
483        return nci;
484    }
485
486    /**
487     * Save all appropriate fragment state.
488     */
489    @Override
490    protected void onSaveInstanceState(Bundle outState) {
491        super.onSaveInstanceState(outState);
492        Parcelable p = mFragments.saveAllState();
493        if (p != null) {
494            outState.putParcelable(FRAGMENTS_TAG, p);
495        }
496    }
497
498    /**
499     * Dispatch onStart() to all fragments.  Ensure any created loaders are
500     * now started.
501     */
502    @Override
503    protected void onStart() {
504        super.onStart();
505
506        mStopped = false;
507        mReallyStopped = false;
508        mHandler.removeMessages(MSG_REALLY_STOPPED);
509
510        if (!mCreated) {
511            mCreated = true;
512            mFragments.dispatchActivityCreated();
513        }
514
515        mFragments.noteStateNotSaved();
516        mFragments.execPendingActions();
517
518        mFragments.doLoaderStart();
519
520        // NOTE: HC onStart goes here.
521
522        mFragments.dispatchStart();
523        mFragments.reportLoaderStart();
524    }
525
526    /**
527     * Dispatch onStop() to all fragments.  Ensure all loaders are stopped.
528     */
529    @Override
530    protected void onStop() {
531        super.onStop();
532
533        mStopped = true;
534        mHandler.sendEmptyMessage(MSG_REALLY_STOPPED);
535
536        mFragments.dispatchStop();
537    }
538
539    // ------------------------------------------------------------------------
540    // NEW METHODS
541    // ------------------------------------------------------------------------
542
543    /**
544     * Use this instead of {@link #onRetainNonConfigurationInstance()}.
545     * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}.
546     */
547    public Object onRetainCustomNonConfigurationInstance() {
548        return null;
549    }
550
551    /**
552     * Return the value previously returned from
553     * {@link #onRetainCustomNonConfigurationInstance()}.
554     */
555    @SuppressWarnings("deprecation")
556    public Object getLastCustomNonConfigurationInstance() {
557        NonConfigurationInstances nc = (NonConfigurationInstances)
558                getLastNonConfigurationInstance();
559        return nc != null ? nc.custom : null;
560    }
561
562    /**
563     * Support library version of {@link Activity#invalidateOptionsMenu}.
564     *
565     * <p>Invalidate the activity's options menu. This will cause relevant presentations
566     * of the menu to fully update via calls to onCreateOptionsMenu and
567     * onPrepareOptionsMenu the next time the menu is requested.
568     */
569    public void supportInvalidateOptionsMenu() {
570        if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
571            // If we are running on HC or greater, we can use the framework
572            // API to invalidate the options menu.
573            ActivityCompatHoneycomb.invalidateOptionsMenu(this);
574            return;
575        }
576
577        // Whoops, older platform...  we'll use a hack, to manually rebuild
578        // the options menu the next time it is prepared.
579        mOptionsMenuInvalidated = true;
580    }
581
582    /**
583     * Print the Activity's state into the given stream.  This gets invoked if
584     * you run "adb shell dumpsys activity <activity_component_name>".
585     *
586     * @param prefix Desired prefix to prepend at each line of output.
587     * @param fd The raw file descriptor that the dump is being sent to.
588     * @param writer The PrintWriter to which you should dump your state.  This will be
589     * closed for you after you return.
590     * @param args additional arguments to the dump request.
591     */
592    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
593        if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
594            // XXX This can only work if we can call the super-class impl. :/
595            //ActivityCompatHoneycomb.dump(this, prefix, fd, writer, args);
596        }
597        writer.print(prefix); writer.print("Local FragmentActivity ");
598                writer.print(Integer.toHexString(System.identityHashCode(this)));
599                writer.println(" State:");
600        String innerPrefix = prefix + "  ";
601        writer.print(innerPrefix); writer.print("mCreated=");
602                writer.print(mCreated); writer.print("mResumed=");
603                writer.print(mResumed); writer.print(" mStopped=");
604                writer.print(mStopped); writer.print(" mReallyStopped=");
605                writer.println(mReallyStopped);
606        mFragments.dumpLoaders(innerPrefix, fd, writer, args);
607        mFragments.getSupportFragmentManager().dump(prefix, fd, writer, args);
608        writer.print(prefix); writer.println("View Hierarchy:");
609        dumpViewHierarchy(prefix + "  ", writer, getWindow().getDecorView());
610    }
611
612    private static String viewToString(View view) {
613        StringBuilder out = new StringBuilder(128);
614        out.append(view.getClass().getName());
615        out.append('{');
616        out.append(Integer.toHexString(System.identityHashCode(view)));
617        out.append(' ');
618        switch (view.getVisibility()) {
619            case View.VISIBLE: out.append('V'); break;
620            case View.INVISIBLE: out.append('I'); break;
621            case View.GONE: out.append('G'); break;
622            default: out.append('.'); break;
623        }
624        out.append(view.isFocusable() ? 'F' : '.');
625        out.append(view.isEnabled() ? 'E' : '.');
626        out.append(view.willNotDraw() ? '.' : 'D');
627        out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
628        out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
629        out.append(view.isClickable() ? 'C' : '.');
630        out.append(view.isLongClickable() ? 'L' : '.');
631        out.append(' ');
632        out.append(view.isFocused() ? 'F' : '.');
633        out.append(view.isSelected() ? 'S' : '.');
634        out.append(view.isPressed() ? 'P' : '.');
635        out.append(' ');
636        out.append(view.getLeft());
637        out.append(',');
638        out.append(view.getTop());
639        out.append('-');
640        out.append(view.getRight());
641        out.append(',');
642        out.append(view.getBottom());
643        final int id = view.getId();
644        if (id != View.NO_ID) {
645            out.append(" #");
646            out.append(Integer.toHexString(id));
647            final Resources r = view.getResources();
648            if (id != 0 && r != null) {
649                try {
650                    String pkgname;
651                    switch (id&0xff000000) {
652                        case 0x7f000000:
653                            pkgname="app";
654                            break;
655                        case 0x01000000:
656                            pkgname="android";
657                            break;
658                        default:
659                            pkgname = r.getResourcePackageName(id);
660                            break;
661                    }
662                    String typename = r.getResourceTypeName(id);
663                    String entryname = r.getResourceEntryName(id);
664                    out.append(" ");
665                    out.append(pkgname);
666                    out.append(":");
667                    out.append(typename);
668                    out.append("/");
669                    out.append(entryname);
670                } catch (Resources.NotFoundException e) {
671                }
672            }
673        }
674        out.append("}");
675        return out.toString();
676    }
677
678    private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) {
679        writer.print(prefix);
680        if (view == null) {
681            writer.println("null");
682            return;
683        }
684        writer.println(viewToString(view));
685        if (!(view instanceof ViewGroup)) {
686            return;
687        }
688        ViewGroup grp = (ViewGroup)view;
689        final int N = grp.getChildCount();
690        if (N <= 0) {
691            return;
692        }
693        prefix = prefix + "  ";
694        for (int i=0; i<N; i++) {
695            dumpViewHierarchy(prefix, writer, grp.getChildAt(i));
696        }
697    }
698
699    void doReallyStop(boolean retaining) {
700        if (!mReallyStopped) {
701            mReallyStopped = true;
702            mRetaining = retaining;
703            mHandler.removeMessages(MSG_REALLY_STOPPED);
704            onReallyStop();
705        }
706    }
707
708    /**
709     * Pre-HC, we didn't have a way to determine whether an activity was
710     * being stopped for a config change or not until we saw
711     * onRetainNonConfigurationInstance() called after onStop().  However
712     * we need to know this, to know whether to retain fragments.  This will
713     * tell us what we need to know.
714     */
715    void onReallyStop() {
716        mFragments.doLoaderStop(mRetaining);
717
718        mFragments.dispatchReallyStop();
719    }
720
721    // ------------------------------------------------------------------------
722    // FRAGMENT SUPPORT
723    // ------------------------------------------------------------------------
724
725    /**
726     * Called when a fragment is attached to the activity.
727     */
728    @SuppressWarnings("unused")
729    public void onAttachFragment(Fragment fragment) {
730    }
731
732    /**
733     * Return the FragmentManager for interacting with fragments associated
734     * with this activity.
735     */
736    public FragmentManager getSupportFragmentManager() {
737        return mFragments.getSupportFragmentManager();
738    }
739
740    public LoaderManager getSupportLoaderManager() {
741        return mFragments.getSupportLoaderManager();
742    }
743
744    /**
745     * Modifies the standard behavior to allow results to be delivered to fragments.
746     * This imposes a restriction that requestCode be <= 0xffff.
747     */
748    @Override
749    public void startActivityForResult(Intent intent, int requestCode) {
750        if (requestCode != -1 && (requestCode&0xffff0000) != 0) {
751            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
752        }
753        super.startActivityForResult(intent, requestCode);
754    }
755
756    /**
757     * Called by Fragment.startActivityForResult() to implement its behavior.
758     */
759    public void startActivityFromFragment(Fragment fragment, Intent intent,
760            int requestCode) {
761        if (requestCode == -1) {
762            super.startActivityForResult(intent, -1);
763            return;
764        }
765        if ((requestCode&0xffff0000) != 0) {
766            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
767        }
768        super.startActivityForResult(intent, ((fragment.mIndex+1)<<16) + (requestCode&0xffff));
769    }
770
771    class HostCallbacks extends FragmentHostCallbacks {
772        public HostCallbacks() {
773            super(FragmentActivity.this /*fragmentActivity*/);
774        }
775
776        @Override
777        public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
778            FragmentActivity.this.dump(prefix, fd, writer, args);
779        }
780
781        @Override
782        public boolean shouldSaveFragmentState(Fragment fragment) {
783            return !isFinishing();
784        }
785
786        @Override
787        public LayoutInflater getLayoutInflater() {
788            return FragmentActivity.this.getLayoutInflater().cloneInContext(FragmentActivity.this);
789        }
790
791        @Override
792        public void supportInvalidateOptionsMenu() {
793            FragmentActivity.this.supportInvalidateOptionsMenu();
794        }
795
796        @Override
797        public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode) {
798            FragmentActivity.this.startActivityFromFragment(fragment, intent, requestCode);
799        }
800
801        @Override
802        public boolean hasWindowAnimations() {
803            return getWindow() != null;
804        }
805
806        @Override
807        public int getWindowAnimations() {
808            final Window w = getWindow();
809            return (w == null) ? 0 : w.getAttributes().windowAnimations;
810        }
811
812        @Nullable
813        @Override
814        public View findViewById(int id) {
815            return FragmentActivity.this.findViewById(id);
816        }
817
818        @Override
819        public boolean hasView() {
820            final Window w = getWindow();
821            return (w != null && w.peekDecorView() != null);
822        }
823    }
824}
825