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