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