FragmentActivity.java revision 7516316ffdf6fbf7b248c615404831191daa66a4
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.app;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.Message;
27import android.os.Parcelable;
28import android.support.annotation.NonNull;
29import android.support.v4.util.SimpleArrayMap;
30import android.util.AttributeSet;
31import android.util.Log;
32import android.view.KeyEvent;
33import android.view.Menu;
34import android.view.MenuItem;
35import android.view.View;
36import android.view.ViewGroup;
37import android.view.Window;
38
39import java.io.FileDescriptor;
40import java.io.PrintWriter;
41import java.util.ArrayList;
42
43/**
44 * Base class for activities that want to use the support-based
45 * {@link android.support.v4.app.Fragment} and
46 * {@link android.support.v4.content.Loader} APIs.
47 *
48 * <p>When using this class as opposed to new platform's built-in fragment
49 * and loader support, you must use the {@link #getSupportFragmentManager()}
50 * and {@link #getSupportLoaderManager()} methods respectively to access
51 * those features.
52 *
53 * <p class="note"><strong>Note:</strong> If you want to implement an activity that includes
54 * an <a href="{@docRoot}guide/topics/ui/actionbar.html">action bar</a>, you should instead use
55 * the {@link android.support.v7.app.ActionBarActivity} class, which is a subclass of this one,
56 * so allows you to use {@link android.support.v4.app.Fragment} APIs on API level 7 and higher.</p>
57 *
58 * <p>Known limitations:</p>
59 * <ul>
60 * <li> <p>When using the <code>&lt;fragment></code> tag, this implementation can not
61 * use the parent view's ID as the new fragment's ID.  You must explicitly
62 * specify an ID (or tag) in the <code>&lt;fragment></code>.</p>
63 * <li> <p>Prior to Honeycomb (3.0), an activity's state was saved before pausing.
64 * Fragments are a significant amount of new state, and dynamic enough that one
65 * often wants them to change between pausing and stopping.  These classes
66 * throw an exception if you try to change the fragment state after it has been
67 * saved, to avoid accidental loss of UI state.  However this is too restrictive
68 * prior to Honeycomb, where the state is saved before pausing.  To address this,
69 * when running on platforms prior to Honeycomb an exception will not be thrown
70 * if you change fragments between the state save and the activity being stopped.
71 * This means that in some cases if the activity is restored from its last saved
72 * state, this may be a snapshot slightly before what the user last saw.</p>
73 * </ul>
74 */
75public class FragmentActivity extends Activity {
76    private static final String TAG = "FragmentActivity";
77
78    static final String FRAGMENTS_TAG = "android:support:fragments";
79
80    // This is the SDK API version of Honeycomb (3.0).
81    private static final int HONEYCOMB = 11;
82
83    static final int MSG_REALLY_STOPPED = 1;
84    static final int MSG_RESUME_PENDING = 2;
85
86    final Handler mHandler = new Handler() {
87        @Override
88        public void handleMessage(Message msg) {
89            switch (msg.what) {
90                case MSG_REALLY_STOPPED:
91                    if (mStopped) {
92                        doReallyStop(false);
93                    }
94                    break;
95                case MSG_RESUME_PENDING:
96                    onResumeFragments();
97                    mFragments.execPendingActions();
98                    break;
99                default:
100                    super.handleMessage(msg);
101            }
102        }
103
104    };
105    final FragmentManagerImpl mFragments = new FragmentManagerImpl();
106    final FragmentContainer mContainer = new FragmentContainer() {
107        @Override
108        public View findViewById(int id) {
109            return FragmentActivity.this.findViewById(id);
110        }
111
112        @Override
113        public boolean hasView() {
114            Window window = FragmentActivity.this.getWindow();
115            return (window != null && window.peekDecorView() != null);
116        }
117    };
118
119    boolean mCreated;
120    boolean mResumed;
121    boolean mStopped;
122    boolean mReallyStopped;
123    boolean mRetaining;
124
125    boolean mOptionsMenuInvalidated;
126
127    boolean mCheckedForLoaderManager;
128    boolean mLoadersStarted;
129    SimpleArrayMap<String, LoaderManagerImpl> mAllLoaderManagers;
130    LoaderManagerImpl mLoaderManager;
131
132    static final class NonConfigurationInstances {
133        Object activity;
134        Object custom;
135        SimpleArrayMap<String, Object> children;
136        ArrayList<Fragment> fragments;
137        SimpleArrayMap<String, LoaderManagerImpl> loaders;
138    }
139
140    // ------------------------------------------------------------------------
141    // HOOKS INTO ACTIVITY
142    // ------------------------------------------------------------------------
143
144    /**
145     * Dispatch incoming result to the correct fragment.
146     */
147    @Override
148    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
149        mFragments.noteStateNotSaved();
150        int index = requestCode>>16;
151        if (index != 0) {
152            index--;
153            if (mFragments.mActive == null || index < 0 || index >= mFragments.mActive.size()) {
154                Log.w(TAG, "Activity result fragment index out of range: 0x"
155                        + Integer.toHexString(requestCode));
156                return;
157            }
158            Fragment frag = mFragments.mActive.get(index);
159            if (frag == null) {
160                Log.w(TAG, "Activity result no fragment exists for index: 0x"
161                        + Integer.toHexString(requestCode));
162            } else {
163                frag.onActivityResult(requestCode&0xffff, resultCode, data);
164            }
165            return;
166        }
167
168        super.onActivityResult(requestCode, resultCode, data);
169    }
170
171    /**
172     * Take care of popping the fragment back stack or finishing the activity
173     * as appropriate.
174     */
175    public void onBackPressed() {
176        if (!mFragments.popBackStackImmediate()) {
177            supportFinishAfterTransition();
178        }
179    }
180
181    /**
182     * Reverses the Activity Scene entry Transition and triggers the calling Activity
183     * to reverse its exit Transition. When the exit Transition completes,
184     * {@link #finish()} is called. If no entry Transition was used, finish() is called
185     * immediately and the Activity exit Transition is run.
186     *
187     * <p>On Android 4.4 or lower, this method only finishes the Activity with no
188     * special exit transition.</p>
189     */
190    public void supportFinishAfterTransition() {
191        ActivityCompat.finishAfterTransition(this);
192    }
193
194    /**
195     * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
196     * android.view.View, String)} was used to start an Activity, <var>callback</var>
197     * will be called to handle shared elements on the <i>launched</i> Activity. This requires
198     * {@link Window#FEATURE_CONTENT_TRANSITIONS}.
199     *
200     * @param callback Used to manipulate shared element transitions on the launched Activity.
201     */
202    public void setEnterSharedElementCallback(SharedElementCallback callback) {
203        ActivityCompat.setEnterSharedElementCallback(this, callback);
204    }
205
206    /**
207     * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
208     * android.view.View, String)} was used to start an Activity, <var>listener</var>
209     * will be called to handle shared elements on the <i>launching</i> Activity. Most
210     * calls will only come when returning from the started Activity.
211     * This requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
212     *
213     * @param listener Used to manipulate shared element transitions on the launching Activity.
214     */
215    public void setExitSharedElementCallback(SharedElementCallback listener) {
216        ActivityCompat.setExitSharedElementCallback(this, listener);
217    }
218
219    /**
220     * Support library version of {@link android.app.Activity#postponeEnterTransition()} that works
221     * only on API 21 and later.
222     */
223    public void supportPostponeEnterTransition() {
224        ActivityCompat.postponeEnterTransition(this);
225    }
226
227    /**
228     * Support library version of {@link android.app.Activity#startPostponedEnterTransition()}
229     * that only works with API 21 and later.
230     */
231    public void supportStartPostponedEnterTransition() {
232        ActivityCompat.startPostponedEnterTransition(this);
233    }
234
235    /**
236     * Dispatch configuration change to all fragments.
237     */
238    @Override
239    public void onConfigurationChanged(Configuration newConfig) {
240        super.onConfigurationChanged(newConfig);
241        mFragments.dispatchConfigurationChanged(newConfig);
242    }
243
244    /**
245     * Perform initialization of all fragments and loaders.
246     */
247    @Override
248    protected void onCreate(Bundle savedInstanceState) {
249        mFragments.attachActivity(this, mContainer, null);
250        // Old versions of the platform didn't do this!
251        if (getLayoutInflater().getFactory() == null) {
252            getLayoutInflater().setFactory(this);
253        }
254
255        super.onCreate(savedInstanceState);
256
257        NonConfigurationInstances nc = (NonConfigurationInstances)
258                getLastNonConfigurationInstance();
259        if (nc != null) {
260            mAllLoaderManagers = nc.loaders;
261        }
262        if (savedInstanceState != null) {
263            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
264            mFragments.restoreAllState(p, nc != null ? nc.fragments : null);
265        }
266        mFragments.dispatchCreate();
267    }
268
269    /**
270     * Dispatch to Fragment.onCreateOptionsMenu().
271     */
272    @Override
273    public boolean onCreatePanelMenu(int featureId, Menu menu) {
274        if (featureId == Window.FEATURE_OPTIONS_PANEL) {
275            boolean show = super.onCreatePanelMenu(featureId, menu);
276            show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
277            if (android.os.Build.VERSION.SDK_INT >= HONEYCOMB) {
278                return show;
279            }
280            // Prior to Honeycomb, the framework can't invalidate the options
281            // menu, so we must always say we have one in case the app later
282            // invalidates it and needs to have it shown.
283            return true;
284        }
285        return super.onCreatePanelMenu(featureId, menu);
286    }
287
288    /**
289     * Add support for inflating the &lt;fragment> tag.
290     */
291    @Override
292    public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) {
293        if (!"fragment".equals(name)) {
294            return super.onCreateView(name, context, attrs);
295        }
296
297        final View v = mFragments.onCreateView(name, context, attrs);
298        if (v == null) {
299            return super.onCreateView(name, context, attrs);
300        }
301        return v;
302    }
303
304    /**
305     * Destroy all fragments and loaders.
306     */
307    @Override
308    protected void onDestroy() {
309        super.onDestroy();
310
311        doReallyStop(false);
312
313        mFragments.dispatchDestroy();
314        if (mLoaderManager != null) {
315            mLoaderManager.doDestroy();
316        }
317    }
318
319    /**
320     * Take care of calling onBackPressed() for pre-Eclair platforms.
321     */
322    @Override
323    public boolean onKeyDown(int keyCode, KeyEvent event) {
324        if (android.os.Build.VERSION.SDK_INT < 5 /* ECLAIR */
325                && keyCode == KeyEvent.KEYCODE_BACK
326                && event.getRepeatCount() == 0) {
327            // Take care of calling this method on earlier versions of
328            // the platform where it doesn't exist.
329            onBackPressed();
330            return true;
331        }
332
333        return super.onKeyDown(keyCode, event);
334    }
335
336    /**
337     * Dispatch onLowMemory() to all fragments.
338     */
339    @Override
340    public void onLowMemory() {
341        super.onLowMemory();
342        mFragments.dispatchLowMemory();
343    }
344
345    /**
346     * Dispatch context and options menu to fragments.
347     */
348    @Override
349    public boolean onMenuItemSelected(int featureId, MenuItem item) {
350        if (super.onMenuItemSelected(featureId, item)) {
351            return true;
352        }
353
354        switch (featureId) {
355            case Window.FEATURE_OPTIONS_PANEL:
356                return mFragments.dispatchOptionsItemSelected(item);
357
358            case Window.FEATURE_CONTEXT_MENU:
359                return mFragments.dispatchContextItemSelected(item);
360
361            default:
362                return false;
363        }
364    }
365
366    /**
367     * Call onOptionsMenuClosed() on fragments.
368     */
369    @Override
370    public void onPanelClosed(int featureId, Menu menu) {
371        switch (featureId) {
372            case Window.FEATURE_OPTIONS_PANEL:
373                mFragments.dispatchOptionsMenuClosed(menu);
374                break;
375        }
376        super.onPanelClosed(featureId, menu);
377    }
378
379    /**
380     * Dispatch onPause() to fragments.
381     */
382    @Override
383    protected void onPause() {
384        super.onPause();
385        mResumed = false;
386        if (mHandler.hasMessages(MSG_RESUME_PENDING)) {
387            mHandler.removeMessages(MSG_RESUME_PENDING);
388            onResumeFragments();
389        }
390        mFragments.dispatchPause();
391    }
392
393    /**
394     * Handle onNewIntent() to inform the fragment manager that the
395     * state is not saved.  If you are handling new intents and may be
396     * making changes to the fragment state, you want to be sure to call
397     * through to the super-class here first.  Otherwise, if your state
398     * is saved but the activity is not stopped, you could get an
399     * onNewIntent() call which happens before onResume() and trying to
400     * perform fragment operations at that point will throw IllegalStateException
401     * because the fragment manager thinks the state is still saved.
402     */
403    @Override
404    protected void onNewIntent(Intent intent) {
405        super.onNewIntent(intent);
406        mFragments.noteStateNotSaved();
407    }
408
409    /**
410     * Dispatch onResume() to fragments.  Note that for better inter-operation
411     * with older versions of the platform, at the point of this call the
412     * fragments attached to the activity are <em>not</em> resumed.  This means
413     * that in some cases the previous state may still be saved, not allowing
414     * fragment transactions that modify the state.  To correctly interact
415     * with fragments in their proper state, you should instead override
416     * {@link #onResumeFragments()}.
417     */
418    @Override
419    protected void onResume() {
420        super.onResume();
421        mHandler.sendEmptyMessage(MSG_RESUME_PENDING);
422        mResumed = true;
423        mFragments.execPendingActions();
424    }
425
426    /**
427     * Dispatch onResume() to fragments.
428     */
429    @Override
430    protected void onPostResume() {
431        super.onPostResume();
432        mHandler.removeMessages(MSG_RESUME_PENDING);
433        onResumeFragments();
434        mFragments.execPendingActions();
435    }
436
437    /**
438     * This is the fragment-orientated version of {@link #onResume()} that you
439     * can override to perform operations in the Activity at the same point
440     * where its fragments are resumed.  Be sure to always call through to
441     * the super-class.
442     */
443    protected void onResumeFragments() {
444        mFragments.dispatchResume();
445    }
446
447    /**
448     * Dispatch onPrepareOptionsMenu() to fragments.
449     */
450    @Override
451    public boolean onPreparePanel(int featureId, View view, Menu menu) {
452        if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
453            if (mOptionsMenuInvalidated) {
454                mOptionsMenuInvalidated = false;
455                menu.clear();
456                onCreatePanelMenu(featureId, menu);
457            }
458            boolean goforit = onPrepareOptionsPanel(view, menu);
459            goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
460            return goforit;
461        }
462        return super.onPreparePanel(featureId, view, menu);
463    }
464
465    /**
466     * @hide
467     */
468    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
469        return super.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, view, menu);
470    }
471
472    /**
473     * Retain all appropriate fragment and loader state.  You can NOT
474     * override this yourself!  Use {@link #onRetainCustomNonConfigurationInstance()}
475     * if you want to retain your own state.
476     */
477    @Override
478    public final Object onRetainNonConfigurationInstance() {
479        if (mStopped) {
480            doReallyStop(true);
481        }
482
483        Object custom = onRetainCustomNonConfigurationInstance();
484
485        ArrayList<Fragment> fragments = mFragments.retainNonConfig();
486        boolean retainLoaders = false;
487        if (mAllLoaderManagers != null) {
488            // prune out any loader managers that were already stopped and so
489            // have nothing useful to retain.
490            final int N = mAllLoaderManagers.size();
491            LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
492            for (int i=N-1; i>=0; i--) {
493                loaders[i] = mAllLoaderManagers.valueAt(i);
494            }
495            for (int i=0; i<N; i++) {
496                LoaderManagerImpl lm = loaders[i];
497                if (lm.mRetaining) {
498                    retainLoaders = true;
499                } else {
500                    lm.doDestroy();
501                    mAllLoaderManagers.remove(lm.mWho);
502                }
503            }
504        }
505        if (fragments == null && !retainLoaders && custom == null) {
506            return null;
507        }
508
509        NonConfigurationInstances nci = new NonConfigurationInstances();
510        nci.activity = null;
511        nci.custom = custom;
512        nci.children = null;
513        nci.fragments = fragments;
514        nci.loaders = mAllLoaderManagers;
515        return nci;
516    }
517
518    /**
519     * Save all appropriate fragment state.
520     */
521    @Override
522    protected void onSaveInstanceState(Bundle outState) {
523        super.onSaveInstanceState(outState);
524        Parcelable p = mFragments.saveAllState();
525        if (p != null) {
526            outState.putParcelable(FRAGMENTS_TAG, p);
527        }
528    }
529
530    /**
531     * Dispatch onStart() to all fragments.  Ensure any created loaders are
532     * now started.
533     */
534    @Override
535    protected void onStart() {
536        super.onStart();
537
538        mStopped = false;
539        mReallyStopped = false;
540        mHandler.removeMessages(MSG_REALLY_STOPPED);
541
542        if (!mCreated) {
543            mCreated = true;
544            mFragments.dispatchActivityCreated();
545        }
546
547        mFragments.noteStateNotSaved();
548        mFragments.execPendingActions();
549
550        if (!mLoadersStarted) {
551            mLoadersStarted = true;
552            if (mLoaderManager != null) {
553                mLoaderManager.doStart();
554            } else if (!mCheckedForLoaderManager) {
555                mLoaderManager = getLoaderManager("(root)", mLoadersStarted, false);
556                // the returned loader manager may be a new one, so we have to start it
557                if ((mLoaderManager != null) && (!mLoaderManager.mStarted)) {
558                    mLoaderManager.doStart();
559                }
560            }
561            mCheckedForLoaderManager = true;
562        }
563        // NOTE: HC onStart goes here.
564
565        mFragments.dispatchStart();
566        if (mAllLoaderManagers != null) {
567            final int N = mAllLoaderManagers.size();
568            LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
569            for (int i=N-1; i>=0; i--) {
570                loaders[i] = mAllLoaderManagers.valueAt(i);
571            }
572            for (int i=0; i<N; i++) {
573                LoaderManagerImpl lm = loaders[i];
574                lm.finishRetain();
575                lm.doReportStart();
576            }
577        }
578    }
579
580    /**
581     * Dispatch onStop() to all fragments.  Ensure all loaders are stopped.
582     */
583    @Override
584    protected void onStop() {
585        super.onStop();
586
587        mStopped = true;
588        mHandler.sendEmptyMessage(MSG_REALLY_STOPPED);
589
590        mFragments.dispatchStop();
591    }
592
593    // ------------------------------------------------------------------------
594    // NEW METHODS
595    // ------------------------------------------------------------------------
596
597    /**
598     * Use this instead of {@link #onRetainNonConfigurationInstance()}.
599     * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}.
600     */
601    public Object onRetainCustomNonConfigurationInstance() {
602        return null;
603    }
604
605    /**
606     * Return the value previously returned from
607     * {@link #onRetainCustomNonConfigurationInstance()}.
608     */
609    public Object getLastCustomNonConfigurationInstance() {
610        NonConfigurationInstances nc = (NonConfigurationInstances)
611                getLastNonConfigurationInstance();
612        return nc != null ? nc.custom : null;
613    }
614
615    /**
616     * Support library version of {@link Activity#invalidateOptionsMenu}.
617     *
618     * <p>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("(root)", mLoadersStarted, true);
859        return mLoaderManager;
860    }
861
862    LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) {
863        if (mAllLoaderManagers == null) {
864            mAllLoaderManagers = new SimpleArrayMap<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