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