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