FragmentActivity.java revision 2290993eddf5262a8df7fc9478daed52401e325a
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.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>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    SimpleArrayMap<String, LoaderManagerImpl> mAllLoaderManagers;
119    LoaderManagerImpl mLoaderManager;
120
121    static final class NonConfigurationInstances {
122        Object activity;
123        Object custom;
124        SimpleArrayMap<String, Object> children;
125        ArrayList<Fragment> fragments;
126        SimpleArrayMap<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;
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            final int N = mAllLoaderManagers.size();
495            LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
496            for (int i=N-1; i>=0; i--) {
497                loaders[i] = mAllLoaderManagers.valueAt(i);
498            }
499            for (int i=0; i<N; i++) {
500                LoaderManagerImpl lm = loaders[i];
501                if (lm.mRetaining) {
502                    retainLoaders = true;
503                } else {
504                    lm.doDestroy();
505                    mAllLoaderManagers.remove(lm.mWho);
506                }
507            }
508        }
509        if (fragments == null && !retainLoaders && custom == null) {
510            return null;
511        }
512
513        NonConfigurationInstances nci = new NonConfigurationInstances();
514        nci.activity = null;
515        nci.custom = custom;
516        nci.children = null;
517        nci.fragments = fragments;
518        nci.loaders = mAllLoaderManagers;
519        return nci;
520    }
521
522    /**
523     * Save all appropriate fragment state.
524     */
525    @Override
526    protected void onSaveInstanceState(Bundle outState) {
527        super.onSaveInstanceState(outState);
528        Parcelable p = mFragments.saveAllState();
529        if (p != null) {
530            outState.putParcelable(FRAGMENTS_TAG, p);
531        }
532    }
533
534    /**
535     * Dispatch onStart() to all fragments.  Ensure any created loaders are
536     * now started.
537     */
538    @Override
539    protected void onStart() {
540        super.onStart();
541
542        mStopped = false;
543        mReallyStopped = false;
544        mHandler.removeMessages(MSG_REALLY_STOPPED);
545
546        if (!mCreated) {
547            mCreated = true;
548            mFragments.dispatchActivityCreated();
549        }
550
551        mFragments.noteStateNotSaved();
552        mFragments.execPendingActions();
553
554        if (!mLoadersStarted) {
555            mLoadersStarted = true;
556            if (mLoaderManager != null) {
557                mLoaderManager.doStart();
558            } else if (!mCheckedForLoaderManager) {
559                mLoaderManager = getLoaderManager("(root)", mLoadersStarted, false);
560                // the returned loader manager may be a new one, so we have to start it
561                if ((mLoaderManager != null) && (!mLoaderManager.mStarted)) {
562                    mLoaderManager.doStart();
563                }
564            }
565            mCheckedForLoaderManager = true;
566        }
567        // NOTE: HC onStart goes here.
568
569        mFragments.dispatchStart();
570        if (mAllLoaderManagers != null) {
571            final int N = mAllLoaderManagers.size();
572            LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
573            for (int i=N-1; i>=0; i--) {
574                loaders[i] = mAllLoaderManagers.valueAt(i);
575            }
576            for (int i=0; i<N; i++) {
577                LoaderManagerImpl lm = loaders[i];
578                lm.finishRetain();
579                lm.doReportStart();
580            }
581        }
582    }
583
584    /**
585     * Dispatch onStop() to all fragments.  Ensure all loaders are stopped.
586     */
587    @Override
588    protected void onStop() {
589        super.onStop();
590
591        mStopped = true;
592        mHandler.sendEmptyMessage(MSG_REALLY_STOPPED);
593
594        mFragments.dispatchStop();
595    }
596
597    // ------------------------------------------------------------------------
598    // NEW METHODS
599    // ------------------------------------------------------------------------
600
601    /**
602     * Use this instead of {@link #onRetainNonConfigurationInstance()}.
603     * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}.
604     */
605    public Object onRetainCustomNonConfigurationInstance() {
606        return null;
607    }
608
609    /**
610     * Return the value previously returned from
611     * {@link #onRetainCustomNonConfigurationInstance()}.
612     */
613    public Object getLastCustomNonConfigurationInstance() {
614        NonConfigurationInstances nc = (NonConfigurationInstances)
615                getLastNonConfigurationInstance();
616        return nc != null ? nc.custom : null;
617    }
618
619    /**
620     * Support library version of {@link Activity#invalidateOptionsMenu}.
621     *
622     * <p>Invalidate the activity's options menu. This will cause relevant presentations
623     * of the menu to fully update via calls to onCreateOptionsMenu and
624     * onPrepareOptionsMenu the next time the menu is requested.
625     */
626    public void supportInvalidateOptionsMenu() {
627        if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
628            // If we are running on HC or greater, we can use the framework
629            // API to invalidate the options menu.
630            ActivityCompatHoneycomb.invalidateOptionsMenu(this);
631            return;
632        }
633
634        // Whoops, older platform...  we'll use a hack, to manually rebuild
635        // the options menu the next time it is prepared.
636        mOptionsMenuInvalidated = true;
637    }
638
639    /**
640     * Print the Activity's state into the given stream.  This gets invoked if
641     * you run "adb shell dumpsys activity <activity_component_name>".
642     *
643     * @param prefix Desired prefix to prepend at each line of output.
644     * @param fd The raw file descriptor that the dump is being sent to.
645     * @param writer The PrintWriter to which you should dump your state.  This will be
646     * closed for you after you return.
647     * @param args additional arguments to the dump request.
648     */
649    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
650        if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
651            // XXX This can only work if we can call the super-class impl. :/
652            //ActivityCompatHoneycomb.dump(this, prefix, fd, writer, args);
653        }
654        writer.print(prefix); writer.print("Local FragmentActivity ");
655                writer.print(Integer.toHexString(System.identityHashCode(this)));
656                writer.println(" State:");
657        String innerPrefix = prefix + "  ";
658        writer.print(innerPrefix); writer.print("mCreated=");
659                writer.print(mCreated); writer.print("mResumed=");
660                writer.print(mResumed); writer.print(" mStopped=");
661                writer.print(mStopped); writer.print(" mReallyStopped=");
662                writer.println(mReallyStopped);
663        writer.print(innerPrefix); writer.print("mLoadersStarted=");
664                writer.println(mLoadersStarted);
665        if (mLoaderManager != null) {
666            writer.print(prefix); writer.print("Loader Manager ");
667                    writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager)));
668                    writer.println(":");
669            mLoaderManager.dump(prefix + "  ", fd, writer, args);
670        }
671        mFragments.dump(prefix, fd, writer, args);
672        writer.print(prefix); writer.println("View Hierarchy:");
673        dumpViewHierarchy(prefix + "  ", writer, getWindow().getDecorView());
674    }
675
676    private static String viewToString(View view) {
677        StringBuilder out = new StringBuilder(128);
678        out.append(view.getClass().getName());
679        out.append('{');
680        out.append(Integer.toHexString(System.identityHashCode(view)));
681        out.append(' ');
682        switch (view.getVisibility()) {
683            case View.VISIBLE: out.append('V'); break;
684            case View.INVISIBLE: out.append('I'); break;
685            case View.GONE: out.append('G'); break;
686            default: out.append('.'); break;
687        }
688        out.append(view.isFocusable() ? 'F' : '.');
689        out.append(view.isEnabled() ? 'E' : '.');
690        out.append(view.willNotDraw() ? '.' : 'D');
691        out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
692        out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
693        out.append(view.isClickable() ? 'C' : '.');
694        out.append(view.isLongClickable() ? 'L' : '.');
695        out.append(' ');
696        out.append(view.isFocused() ? 'F' : '.');
697        out.append(view.isSelected() ? 'S' : '.');
698        out.append(view.isPressed() ? 'P' : '.');
699        out.append(' ');
700        out.append(view.getLeft());
701        out.append(',');
702        out.append(view.getTop());
703        out.append('-');
704        out.append(view.getRight());
705        out.append(',');
706        out.append(view.getBottom());
707        final int id = view.getId();
708        if (id != View.NO_ID) {
709            out.append(" #");
710            out.append(Integer.toHexString(id));
711            final Resources r = view.getResources();
712            if (id != 0 && r != null) {
713                try {
714                    String pkgname;
715                    switch (id&0xff000000) {
716                        case 0x7f000000:
717                            pkgname="app";
718                            break;
719                        case 0x01000000:
720                            pkgname="android";
721                            break;
722                        default:
723                            pkgname = r.getResourcePackageName(id);
724                            break;
725                    }
726                    String typename = r.getResourceTypeName(id);
727                    String entryname = r.getResourceEntryName(id);
728                    out.append(" ");
729                    out.append(pkgname);
730                    out.append(":");
731                    out.append(typename);
732                    out.append("/");
733                    out.append(entryname);
734                } catch (Resources.NotFoundException e) {
735                }
736            }
737        }
738        out.append("}");
739        return out.toString();
740    }
741
742    private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) {
743        writer.print(prefix);
744        if (view == null) {
745            writer.println("null");
746            return;
747        }
748        writer.println(viewToString(view));
749        if (!(view instanceof ViewGroup)) {
750            return;
751        }
752        ViewGroup grp = (ViewGroup)view;
753        final int N = grp.getChildCount();
754        if (N <= 0) {
755            return;
756        }
757        prefix = prefix + "  ";
758        for (int i=0; i<N; i++) {
759            dumpViewHierarchy(prefix, writer, grp.getChildAt(i));
760        }
761    }
762
763    void doReallyStop(boolean retaining) {
764        if (!mReallyStopped) {
765            mReallyStopped = true;
766            mRetaining = retaining;
767            mHandler.removeMessages(MSG_REALLY_STOPPED);
768            onReallyStop();
769        }
770    }
771
772    /**
773     * Pre-HC, we didn't have a way to determine whether an activity was
774     * being stopped for a config change or not until we saw
775     * onRetainNonConfigurationInstance() called after onStop().  However
776     * we need to know this, to know whether to retain fragments.  This will
777     * tell us what we need to know.
778     */
779    void onReallyStop() {
780        if (mLoadersStarted) {
781            mLoadersStarted = false;
782            if (mLoaderManager != null) {
783                if (!mRetaining) {
784                    mLoaderManager.doStop();
785                } else {
786                    mLoaderManager.doRetain();
787                }
788            }
789        }
790
791        mFragments.dispatchReallyStop();
792    }
793
794    // ------------------------------------------------------------------------
795    // FRAGMENT SUPPORT
796    // ------------------------------------------------------------------------
797
798    /**
799     * Called when a fragment is attached to the activity.
800     */
801    public void onAttachFragment(Fragment fragment) {
802    }
803
804    /**
805     * Return the FragmentManager for interacting with fragments associated
806     * with this activity.
807     */
808    public FragmentManager getSupportFragmentManager() {
809        return mFragments;
810    }
811
812    /**
813     * Modifies the standard behavior to allow results to be delivered to fragments.
814     * This imposes a restriction that requestCode be <= 0xffff.
815     */
816    @Override
817    public void startActivityForResult(Intent intent, int requestCode) {
818        if (requestCode != -1 && (requestCode&0xffff0000) != 0) {
819            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
820        }
821        super.startActivityForResult(intent, requestCode);
822    }
823
824    /**
825     * Called by Fragment.startActivityForResult() to implement its behavior.
826     */
827    public void startActivityFromFragment(Fragment fragment, Intent intent,
828            int requestCode) {
829        if (requestCode == -1) {
830            super.startActivityForResult(intent, -1);
831            return;
832        }
833        if ((requestCode&0xffff0000) != 0) {
834            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
835        }
836        super.startActivityForResult(intent, ((fragment.mIndex+1)<<16) + (requestCode&0xffff));
837    }
838
839    void invalidateSupportFragment(String who) {
840        //Log.v(TAG, "invalidateSupportFragment: who=" + who);
841        if (mAllLoaderManagers != null) {
842            LoaderManagerImpl lm = mAllLoaderManagers.get(who);
843            if (lm != null && !lm.mRetaining) {
844                lm.doDestroy();
845                mAllLoaderManagers.remove(who);
846            }
847        }
848    }
849
850    // ------------------------------------------------------------------------
851    // LOADER SUPPORT
852    // ------------------------------------------------------------------------
853
854    /**
855     * Return the LoaderManager for this fragment, creating it if needed.
856     */
857    public LoaderManager getSupportLoaderManager() {
858        if (mLoaderManager != null) {
859            return mLoaderManager;
860        }
861        mCheckedForLoaderManager = true;
862        mLoaderManager = getLoaderManager("(root)", mLoadersStarted, true);
863        return mLoaderManager;
864    }
865
866    LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) {
867        if (mAllLoaderManagers == null) {
868            mAllLoaderManagers = new SimpleArrayMap<String, LoaderManagerImpl>();
869        }
870        LoaderManagerImpl lm = mAllLoaderManagers.get(who);
871        if (lm == null) {
872            if (create) {
873                lm = new LoaderManagerImpl(who, this, started);
874                mAllLoaderManagers.put(who, lm);
875            }
876        } else {
877            lm.updateActivity(this);
878        }
879        return lm;
880    }
881}
882