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