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