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