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