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