1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/* 2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project 3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License"); 5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License. 6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at 7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * http://www.apache.org/licenses/LICENSE-2.0 9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software 11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS, 12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and 14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License. 15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.app; 18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 199a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikasimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 209a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikas 21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.app.Activity; 22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Context; 23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Intent; 24fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarriimport android.content.IntentSender; 25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.res.Configuration; 2647844337986f365dae294fb434de0c0f7f8df5dbGeorge Mountimport android.os.Build; 27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle; 28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler; 29681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackbornimport android.os.Message; 30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcelable; 316ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powellimport android.support.annotation.CallSuper; 32a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.NonNull; 330fb3ec354da6c1f3ed1777bca5270710cc537e92Scott Kennedyimport android.support.annotation.Nullable; 34c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo; 352290993eddf5262a8df7fc9478daed52401e325aDianne Hackbornimport android.support.v4.util.SimpleArrayMap; 36a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnickimport android.support.v4.util.SparseArrayCompat; 37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.AttributeSet; 38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.Log; 39d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedyimport android.view.LayoutInflater; 40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Menu; 41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuItem; 42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View; 43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Window; 44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.FileDescriptor; 46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.PrintWriter; 47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 490574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * Base class for activities that want to use the support-based 507dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@link android.support.v4.app.Fragment} and 517dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@link android.support.v4.content.Loader} APIs. 527dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 537dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>When using this class as opposed to new platform's built-in fragment 547dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * and loader support, you must use the {@link #getSupportFragmentManager()} 557dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * and {@link #getSupportLoaderManager()} methods respectively to access 567dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * those features. 57681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * 5827aea04b07c1fafa0f815aa4f80374a9e051b41cDianne Hackborn * <p>Known limitations:</p> 59681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * <ul> 609dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <li> <p>When using the <code><fragment></code> tag, this implementation can not 61681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * use the parent view's ID as the new fragment's ID. You must explicitly 629dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * specify an ID (or tag) in the <code><fragment></code>.</p> 63681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * </ul> 64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 65434607fedf2615b20b5669f240b46cb09a167cafAurimas Liutikaspublic class FragmentActivity extends BaseFragmentActivityApi16 implements 66e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav ActivityCompat.OnRequestPermissionsResultCallback, 673681134daf188176bc4c99e17ecb94c2f050d8c0Aurimas Liutikas ActivityCompat.RequestPermissionsRequestCodeValidator { 68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private static final String TAG = "FragmentActivity"; 69d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 700adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn static final String FRAGMENTS_TAG = "android:support:fragments"; 71a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick static final String NEXT_CANDIDATE_REQUEST_INDEX_TAG = "android:support:next_request_index"; 72a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick static final String ALLOCATED_REQUEST_INDICIES_TAG = "android:support:request_indicies"; 73a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick static final String REQUEST_FRAGMENT_WHO_TAG = "android:support:request_fragment_who"; 74a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick static final int MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS = 0xffff - 1; 75d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 76681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn static final int MSG_REALLY_STOPPED = 1; 77ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn static final int MSG_RESUME_PENDING = 2; 78681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 79681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn final Handler mHandler = new Handler() { 80681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn @Override 81681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn public void handleMessage(Message msg) { 82681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn switch (msg.what) { 83681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn case MSG_REALLY_STOPPED: 84681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn if (mStopped) { 85681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn doReallyStop(false); 86681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 87681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn break; 88ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn case MSG_RESUME_PENDING: 892c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn onResumeFragments(); 90ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mFragments.execPendingActions(); 91ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn break; 92681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn default: 93681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn super.handleMessage(msg); 94681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 95681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 96681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 97681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn }; 98d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy final FragmentController mFragments = FragmentController.createController(new HostCallbacks()); 997516316ffdf6fbf7b248c615404831191daa66a4George Mount 100ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn boolean mCreated; 101681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn boolean mResumed; 102eb87508275fbb5d9bbce0d363a0de2f913b116dfGeorge Mount boolean mStopped = true; 103eb87508275fbb5d9bbce0d363a0de2f913b116dfGeorge Mount boolean mReallyStopped = true; 104218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn boolean mRetaining; 105681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 106e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav boolean mRequestedPermissionsFromFragment; 107a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 108a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // A hint for the next candidate request index. Request indicies are ints between 0 and 2^16-1 109a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // which are encoded into the upper 16 bits of the requestCode for 110a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // Fragment.startActivityForResult(...) calls. This allows us to dispatch onActivityResult(...) 111a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // to the appropriate Fragment. Request indicies are allocated by allocateRequestIndex(...). 112a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick int mNextCandidateRequestIndex; 113a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // A map from request index to Fragment "who" (i.e. a Fragment's unique identifier). Used to 114a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // keep track of the originating Fragment for Fragment.startActivityForResult(...) calls, so we 115a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // can dispatch the onActivityResult(...) to the appropriate Fragment. Will only contain entries 116a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // for startActivityForResult calls where a result has not yet been delivered. 117a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick SparseArrayCompat<String> mPendingFragmentActivityResults; 11827aea04b07c1fafa0f815aa4f80374a9e051b41cDianne Hackborn 119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final class NonConfigurationInstances { 120b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn Object custom; 121c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell FragmentManagerNonConfig fragments; 122d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy SimpleArrayMap<String, LoaderManager> loaders; 123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 124d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // ------------------------------------------------------------------------ 126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // HOOKS INTO ACTIVITY 127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // ------------------------------------------------------------------------ 128d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch incoming result to the correct fragment. 131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onActivityResult(int requestCode, int resultCode, Intent data) { 1344ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn mFragments.noteStateNotSaved(); 135a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick int requestIndex = requestCode>>16; 136a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (requestIndex != 0) { 137a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick requestIndex--; 138a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 139a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick String who = mPendingFragmentActivityResults.get(requestIndex); 140a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mPendingFragmentActivityResults.remove(requestIndex); 141a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (who == null) { 142a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick Log.w(TAG, "Activity result delivered for unknown Fragment."); 143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 145a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick Fragment targetFragment = mFragments.findFragmentByWho(who); 146a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (targetFragment == null) { 147a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick Log.w(TAG, "Activity result no fragment exists for who: " + who); 1488205f0588fe605821216e4b9fe2c26709c4db8beJake Wharton } else { 149fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri targetFragment.onActivityResult(requestCode & 0xffff, resultCode, data); 150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 153d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onActivityResult(requestCode, resultCode, data); 155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Take care of popping the fragment back stack or finishing the activity 159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * as appropriate. 160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 16190ac236504c1a4cac7e91f1ffc523334f2a8f399Aurimas Liutikas @Override 162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onBackPressed() { 16347844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount FragmentManager fragmentManager = mFragments.getSupportFragmentManager(); 16447844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount final boolean isStateSaved = fragmentManager.isStateSaved(); 16547844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount if (isStateSaved && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) { 16647844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount // Older versions will throw an exception from the framework 16747844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount // FragmentManager.popBackStackImmediate(), so we'll just 16847844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount // return here. The Activity is likely already on its way out 16947844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount // since the fragmentManager has already been saved. 17047844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount return; 17147844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount } 17247844337986f365dae294fb434de0c0f7f8df5dbGeorge Mount if (isStateSaved || !fragmentManager.popBackStackImmediate()) { 17381fc7d74aa951af803aeab9087455c2d2e027136Kirill Grouchnikov super.onBackPressed(); 174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 178559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * Reverses the Activity Scene entry Transition and triggers the calling Activity 179559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * to reverse its exit Transition. When the exit Transition completes, 180559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * {@link #finish()} is called. If no entry Transition was used, finish() is called 181559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * immediately and the Activity exit Transition is run. 182559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * 183559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * <p>On Android 4.4 or lower, this method only finishes the Activity with no 184559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * special exit transition.</p> 185559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount */ 18660afe0bb9ee626e5b48289a20a341c6066a64dc8George Mount public void supportFinishAfterTransition() { 187559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount ActivityCompat.finishAfterTransition(this); 188559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount } 189559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount 190559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount /** 191559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, 1929306f07b1d66f4974a81f781d29739b30124fff0George Mount * android.view.View, String)} was used to start an Activity, <var>callback</var> 193559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * will be called to handle shared elements on the <i>launched</i> Activity. This requires 194559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * {@link Window#FEATURE_CONTENT_TRANSITIONS}. 195559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * 1969306f07b1d66f4974a81f781d29739b30124fff0George Mount * @param callback Used to manipulate shared element transitions on the launched Activity. 197559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount */ 1989306f07b1d66f4974a81f781d29739b30124fff0George Mount public void setEnterSharedElementCallback(SharedElementCallback callback) { 1999306f07b1d66f4974a81f781d29739b30124fff0George Mount ActivityCompat.setEnterSharedElementCallback(this, callback); 200559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount } 201559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount 202559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount /** 203559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, 204559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * android.view.View, String)} was used to start an Activity, <var>listener</var> 205559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * will be called to handle shared elements on the <i>launching</i> Activity. Most 206559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * calls will only come when returning from the started Activity. 207559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * This requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. 208559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * 209559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * @param listener Used to manipulate shared element transitions on the launching Activity. 210559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount */ 2119306f07b1d66f4974a81f781d29739b30124fff0George Mount public void setExitSharedElementCallback(SharedElementCallback listener) { 2129306f07b1d66f4974a81f781d29739b30124fff0George Mount ActivityCompat.setExitSharedElementCallback(this, listener); 213559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount } 214559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount 215559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount /** 2168b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount * Support library version of {@link android.app.Activity#postponeEnterTransition()} that works 2178b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount * only on API 21 and later. 2188b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount */ 2198b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount public void supportPostponeEnterTransition() { 2208b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount ActivityCompat.postponeEnterTransition(this); 2218b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount } 2228b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount 2238b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount /** 2248b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount * Support library version of {@link android.app.Activity#startPostponedEnterTransition()} 2258b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount * that only works with API 21 and later. 2268b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount */ 2278b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount public void supportStartPostponedEnterTransition() { 2288b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount ActivityCompat.startPostponedEnterTransition(this); 2298b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount } 2305fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian 2315fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian /** 2326ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * {@inheritDoc} 2335fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian * 2346ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * <p><strong>Note:</strong> If you override this method you must call 2356ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * <code>super.onMultiWindowModeChanged</code> to correctly dispatch the event 2366ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * to support fragments attached to this activity.</p> 2376ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * 2386ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * @param isInMultiWindowMode True if the activity is in multi-window mode. 2395fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian */ 240e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 2416ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell @CallSuper 2426ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { 2435fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode); 2445fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian } 2455fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian 2465fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian /** 2476ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * {@inheritDoc} 2485fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian * 2496ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * <p><strong>Note:</strong> If you override this method you must call 2506ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * <code>super.onPictureInPictureModeChanged</code> to correctly dispatch the event 2516ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * to support fragments attached to this activity.</p> 2526ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * 2536ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode. 2545fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian */ 255e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 2566ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell @CallSuper 2576ed38774de1111d022cc878e7ee0cd1b0875176cAdam Powell public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 2585fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); 2595fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian } 2608b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount 2618b8a369d50ff43ad2a8836e66283c2bca9c2711eGeorge Mount /** 262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch configuration change to all fragments. 263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onConfigurationChanged(Configuration newConfig) { 266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onConfigurationChanged(newConfig); 267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchConfigurationChanged(newConfig); 268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Perform initialization of all fragments and loaders. 272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 273d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @SuppressWarnings("deprecation") 274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 275c861fd8b28b0b878ccc194ee6099b3b1bc4c3ae8Scott Kennedy protected void onCreate(@Nullable Bundle savedInstanceState) { 276d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.attachHost(null /*parent*/); 277d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onCreate(savedInstanceState); 279d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 280d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy NonConfigurationInstances nc = 281d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy (NonConfigurationInstances) getLastNonConfigurationInstance(); 282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (nc != null) { 283d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.restoreLoaderNonConfig(nc.loaders); 284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (savedInstanceState != null) { 286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG); 287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.restoreAllState(p, nc != null ? nc.fragments : null); 288a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 289a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // Check if there are any pending onActivityResult calls to descendent Fragments. 290a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (savedInstanceState.containsKey(NEXT_CANDIDATE_REQUEST_INDEX_TAG)) { 291a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mNextCandidateRequestIndex = 292a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick savedInstanceState.getInt(NEXT_CANDIDATE_REQUEST_INDEX_TAG); 293a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick int[] requestCodes = savedInstanceState.getIntArray(ALLOCATED_REQUEST_INDICIES_TAG); 294a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick String[] fragmentWhos = savedInstanceState.getStringArray(REQUEST_FRAGMENT_WHO_TAG); 295a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (requestCodes == null || fragmentWhos == null || 296a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick requestCodes.length != fragmentWhos.length) { 297a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick Log.w(TAG, "Invalid requestCode mapping in savedInstanceState."); 298a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } else { 299a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mPendingFragmentActivityResults = new SparseArrayCompat<>(requestCodes.length); 300a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick for (int i = 0; i < requestCodes.length; i++) { 301a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mPendingFragmentActivityResults.put(requestCodes[i], fragmentWhos[i]); 302a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 303a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 304a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 305a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 306a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 307a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (mPendingFragmentActivityResults == null) { 308a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mPendingFragmentActivityResults = new SparseArrayCompat<>(); 309a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mNextCandidateRequestIndex = 0; 310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 311a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchCreate(); 313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch to Fragment.onCreateOptionsMenu(). 317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean onCreatePanelMenu(int featureId, Menu menu) { 320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (featureId == Window.FEATURE_OPTIONS_PANEL) { 321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean show = super.onCreatePanelMenu(featureId, menu); 322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); 3239a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikas return show; 324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return super.onCreatePanelMenu(featureId, menu); 326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 327d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 32920680c49584c26f7818c2dec8fb00633af753e4aChris Banes final View dispatchFragmentsOnCreateView(View parent, String name, Context context, 33020680c49584c26f7818c2dec8fb00633af753e4aChris Banes AttributeSet attrs) { 33120680c49584c26f7818c2dec8fb00633af753e4aChris Banes return mFragments.onCreateView(parent, name, context, attrs); 332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Destroy all fragments and loaders. 336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onDestroy() { 339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onDestroy(); 340681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 341681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn doReallyStop(false); 342681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchDestroy(); 344d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.doLoaderDestroy(); 345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch onLowMemory() to all fragments. 349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onLowMemory() { 352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onLowMemory(); 353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchLowMemory(); 354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch context and options menu to fragments. 358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean onMenuItemSelected(int featureId, MenuItem item) { 361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (super.onMenuItemSelected(featureId, item)) { 362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 364dea7d510a375df0e0e703bfba5ff63c8acca849eAurimas Liutikas 365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (featureId) { 366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Window.FEATURE_OPTIONS_PANEL: 367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mFragments.dispatchOptionsItemSelected(item); 368dea7d510a375df0e0e703bfba5ff63c8acca849eAurimas Liutikas 369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Window.FEATURE_CONTEXT_MENU: 370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mFragments.dispatchContextItemSelected(item); 371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn default: 373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Call onOptionsMenuClosed() on fragments. 379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onPanelClosed(int featureId, Menu menu) { 382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (featureId) { 383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Window.FEATURE_OPTIONS_PANEL: 384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchOptionsMenuClosed(menu); 385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onPanelClosed(featureId, menu); 388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 389dea7d510a375df0e0e703bfba5ff63c8acca849eAurimas Liutikas 390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch onPause() to fragments. 392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onPause() { 395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onPause(); 396681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mResumed = false; 397ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn if (mHandler.hasMessages(MSG_RESUME_PENDING)) { 398ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mHandler.removeMessages(MSG_RESUME_PENDING); 3992c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn onResumeFragments(); 400ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn } 401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchPause(); 402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 4054ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * Handle onNewIntent() to inform the fragment manager that the 4064ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * state is not saved. If you are handling new intents and may be 4074ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * making changes to the fragment state, you want to be sure to call 4084ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * through to the super-class here first. Otherwise, if your state 4094ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * is saved but the activity is not stopped, you could get an 4104ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * onNewIntent() call which happens before onResume() and trying to 4114ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * perform fragment operations at that point will throw IllegalStateException 4124ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn * because the fragment manager thinks the state is still saved. 4134ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn */ 4144ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn @Override 4154ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn protected void onNewIntent(Intent intent) { 4164ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn super.onNewIntent(intent); 4174ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn mFragments.noteStateNotSaved(); 418296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn } 419296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn 420296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn /** 421296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn * Hook in to note that fragment state is no longer saved. 422296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn */ 423e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 424296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn public void onStateNotSaved() { 425296cc5698f82375b48a9843c8db229693ef1ff6dDianne Hackborn mFragments.noteStateNotSaved(); 4264ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn } 4274ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn 4284ccc001f3f883190ac8d900c4f69d71fda94690eDianne Hackborn /** 4292c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * Dispatch onResume() to fragments. Note that for better inter-operation 4302c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * with older versions of the platform, at the point of this call the 4312c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * fragments attached to the activity are <em>not</em> resumed. This means 4322c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * that in some cases the previous state may still be saved, not allowing 4332c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * fragment transactions that modify the state. To correctly interact 4342c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * with fragments in their proper state, you should instead override 4352c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * {@link #onResumeFragments()}. 436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 438ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn protected void onResume() { 439ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn super.onResume(); 440ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mHandler.sendEmptyMessage(MSG_RESUME_PENDING); 441ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mResumed = true; 442ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mFragments.execPendingActions(); 443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 445cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch onResume() to fragments. 447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 448cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 449cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onPostResume() { 450cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onPostResume(); 451ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mHandler.removeMessages(MSG_RESUME_PENDING); 4522c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn onResumeFragments(); 453cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.execPendingActions(); 454cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 455cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 456cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 4572c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * This is the fragment-orientated version of {@link #onResume()} that you 4582c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * can override to perform operations in the Activity at the same point 4592c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * where its fragments are resumed. Be sure to always call through to 4602c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn * the super-class. 4612c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn */ 4622c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn protected void onResumeFragments() { 4632c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn mFragments.dispatchResume(); 4642c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn } 4652c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn 4662c9657f7b1eabf9bdbaf348d206a76cc7f0a4400Dianne Hackborn /** 467cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch onPrepareOptionsMenu() to fragments. 468cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 469cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 470cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean onPreparePanel(int featureId, View view, Menu menu) { 471cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { 4722bc5191a8cc331b404724759a015949a4b5499fbChris Banes boolean goforit = onPrepareOptionsPanel(view, menu); 473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn goforit |= mFragments.dispatchPrepareOptionsMenu(menu); 474d3de4f6d003d92c2a3c5f449a0d759ab9032d2ffAdam Powell return goforit; 475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return super.onPreparePanel(featureId, view, menu); 477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 478cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 479cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 4802bc5191a8cc331b404724759a015949a4b5499fbChris Banes * @hide 4812bc5191a8cc331b404724759a015949a4b5499fbChris Banes */ 4828e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas @RestrictTo(LIBRARY_GROUP) 4832bc5191a8cc331b404724759a015949a4b5499fbChris Banes protected boolean onPrepareOptionsPanel(View view, Menu menu) { 4842bc5191a8cc331b404724759a015949a4b5499fbChris Banes return super.onPreparePanel(Window.FEATURE_OPTIONS_PANEL, view, menu); 4852bc5191a8cc331b404724759a015949a4b5499fbChris Banes } 4862bc5191a8cc331b404724759a015949a4b5499fbChris Banes 4872bc5191a8cc331b404724759a015949a4b5499fbChris Banes /** 488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Retain all appropriate fragment and loader state. You can NOT 489b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn * override this yourself! Use {@link #onRetainCustomNonConfigurationInstance()} 490b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn * if you want to retain your own state. 491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 492cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 493cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public final Object onRetainNonConfigurationInstance() { 494681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn if (mStopped) { 495681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn doReallyStop(true); 496681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 497681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 498b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn Object custom = onRetainCustomNonConfigurationInstance(); 499b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn 500c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig(); 501d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy SimpleArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig(); 502d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 503d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (fragments == null && loaders == null && custom == null) { 504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 506d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn NonConfigurationInstances nci = new NonConfigurationInstances(); 508b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn nci.custom = custom; 509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn nci.fragments = fragments; 510d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy nci.loaders = loaders; 511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return nci; 512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Save all appropriate fragment state. 516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onSaveInstanceState(Bundle outState) { 519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onSaveInstanceState(outState); 520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Parcelable p = mFragments.saveAllState(); 521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (p != null) { 522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn outState.putParcelable(FRAGMENTS_TAG, p); 523cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 524a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (mPendingFragmentActivityResults.size() > 0) { 525a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick outState.putInt(NEXT_CANDIDATE_REQUEST_INDEX_TAG, mNextCandidateRequestIndex); 526a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 527a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick int[] requestCodes = new int[mPendingFragmentActivityResults.size()]; 528a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick String[] fragmentWhos = new String[mPendingFragmentActivityResults.size()]; 529a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick for (int i = 0; i < mPendingFragmentActivityResults.size(); i++) { 530a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick requestCodes[i] = mPendingFragmentActivityResults.keyAt(i); 531a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick fragmentWhos[i] = mPendingFragmentActivityResults.valueAt(i); 532a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 533a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick outState.putIntArray(ALLOCATED_REQUEST_INDICIES_TAG, requestCodes); 534a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick outState.putStringArray(REQUEST_FRAGMENT_WHO_TAG, fragmentWhos); 535a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 536cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 537cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 538cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 539cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch onStart() to all fragments. Ensure any created loaders are 540cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * now started. 541cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 542cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 543cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onStart() { 544cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onStart(); 545681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 546681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mStopped = false; 547218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn mReallyStopped = false; 548681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mHandler.removeMessages(MSG_REALLY_STOPPED); 549681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 550ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn if (!mCreated) { 551ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mCreated = true; 552ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mFragments.dispatchActivityCreated(); 553ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn } 554ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn 555cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.noteStateNotSaved(); 556cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.execPendingActions(); 557d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 558d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.doLoaderStart(); 559d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 56027aea04b07c1fafa0f815aa4f80374a9e051b41cDianne Hackborn // NOTE: HC onStart goes here. 561d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 562cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchStart(); 563d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.reportLoaderStart(); 564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 566cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 567cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Dispatch onStop() to all fragments. Ensure all loaders are stopped. 568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected void onStop() { 571cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.onStop(); 572681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 573681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mStopped = true; 574681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mHandler.sendEmptyMessage(MSG_REALLY_STOPPED); 575d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 576cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFragments.dispatchStop(); 577cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 578cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 579cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // ------------------------------------------------------------------------ 580cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // NEW METHODS 581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // ------------------------------------------------------------------------ 582d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 583b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn /** 584b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn * Use this instead of {@link #onRetainNonConfigurationInstance()}. 585b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}. 586b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn */ 587b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn public Object onRetainCustomNonConfigurationInstance() { 588b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn return null; 589b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn } 590b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn 591b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn /** 592b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn * Return the value previously returned from 593b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn * {@link #onRetainCustomNonConfigurationInstance()}. 594b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn */ 595d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @SuppressWarnings("deprecation") 596b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn public Object getLastCustomNonConfigurationInstance() { 597b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn NonConfigurationInstances nc = (NonConfigurationInstances) 598b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn getLastNonConfigurationInstance(); 599b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn return nc != null ? nc.custom : null; 600b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn } 601b07179708a404260c65814b0ff14702eef189c01Dianne Hackborn 60297b3c54fa1e1d75a87fd2429606c615894953946Adam Powell /** 603e6072e2d918169bd827cf7431347fb648124c227Jeff Brown * Support library version of {@link Activity#invalidateOptionsMenu}. 604e6072e2d918169bd827cf7431347fb648124c227Jeff Brown * 605e6072e2d918169bd827cf7431347fb648124c227Jeff Brown * <p>Invalidate the activity's options menu. This will cause relevant presentations 60697b3c54fa1e1d75a87fd2429606c615894953946Adam Powell * of the menu to fully update via calls to onCreateOptionsMenu and 60797b3c54fa1e1d75a87fd2429606c615894953946Adam Powell * onPrepareOptionsMenu the next time the menu is requested. 6089a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikas * 6099a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikas * @deprecated Call {@link Activity#invalidateOptionsMenu} directly. 61097b3c54fa1e1d75a87fd2429606c615894953946Adam Powell */ 6119a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikas @Deprecated 61297b3c54fa1e1d75a87fd2429606c615894953946Adam Powell public void supportInvalidateOptionsMenu() { 6139a8ae954f48a89b3519d6a3c029b2061318ce07bAurimas Liutikas invalidateOptionsMenu(); 614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 615e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes 616e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes /** 617cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Print the Activity's state into the given stream. This gets invoked if 618cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you run "adb shell dumpsys activity <activity_component_name>". 619cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 620cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param prefix Desired prefix to prepend at each line of output. 621cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param fd The raw file descriptor that the dump is being sent to. 622cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param writer The PrintWriter to which you should dump your state. This will be 623cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * closed for you after you return. 624cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param args additional arguments to the dump request. 625cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 626e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 627cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 6285ba2dc40bc0b34799e0ffab093b9aca8798eaf25Adam Powell super.dump(prefix, fd, writer, args); 629681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn writer.print(prefix); writer.print("Local FragmentActivity "); 630cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(Integer.toHexString(System.identityHashCode(this))); 631cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(" State:"); 632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String innerPrefix = prefix + " "; 633ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn writer.print(innerPrefix); writer.print("mCreated="); 634ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn writer.print(mCreated); writer.print("mResumed="); 635681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn writer.print(mResumed); writer.print(" mStopped="); 636681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn writer.print(mStopped); writer.print(" mReallyStopped="); 637681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn writer.println(mReallyStopped); 638d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.dumpLoaders(innerPrefix, fd, writer, args); 639d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.getSupportFragmentManager().dump(prefix, fd, writer, args); 640cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 642681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn void doReallyStop(boolean retaining) { 643681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn if (!mReallyStopped) { 644681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mReallyStopped = true; 645218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn mRetaining = retaining; 646681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mHandler.removeMessages(MSG_REALLY_STOPPED); 647218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn onReallyStop(); 648b054427688e7cf0475bec09da9a3fb7688881459Adam Powell } else if (retaining) { 649b054427688e7cf0475bec09da9a3fb7688881459Adam Powell // We're already really stopped, but we've been asked to retain. 650b054427688e7cf0475bec09da9a3fb7688881459Adam Powell // Our fragments are taken care of but we need to mark the loaders for retention. 651b054427688e7cf0475bec09da9a3fb7688881459Adam Powell // In order to do this correctly we need to restart the loaders first before 652b054427688e7cf0475bec09da9a3fb7688881459Adam Powell // handing them off to the next activity. 653b054427688e7cf0475bec09da9a3fb7688881459Adam Powell mFragments.doLoaderStart(); 654b054427688e7cf0475bec09da9a3fb7688881459Adam Powell mFragments.doLoaderStop(true); 655681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 656681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 657681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 658681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn /** 659681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * Pre-HC, we didn't have a way to determine whether an activity was 660681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * being stopped for a config change or not until we saw 661681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * onRetainNonConfigurationInstance() called after onStop(). However 662681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * we need to know this, to know whether to retain fragments. This will 663681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn * tell us what we need to know. 664681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn */ 665218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn void onReallyStop() { 666d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mFragments.doLoaderStop(mRetaining); 667681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 668218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn mFragments.dispatchReallyStop(); 669681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 670681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 671cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // ------------------------------------------------------------------------ 672cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // FRAGMENT SUPPORT 673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // ------------------------------------------------------------------------ 674d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 675cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Called when a fragment is attached to the activity. 677cef09fee2126f901aa164f6e89c370ab81cff1b3Adam Powell * 678cef09fee2126f901aa164f6e89c370ab81cff1b3Adam Powell * <p>This is called after the attached fragment's <code>onAttach</code> and before 679cef09fee2126f901aa164f6e89c370ab81cff1b3Adam Powell * the attached fragment's <code>onCreate</code> if the fragment has not yet had a previous 680cef09fee2126f901aa164f6e89c370ab81cff1b3Adam Powell * call to <code>onCreate</code>.</p> 681cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 682d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @SuppressWarnings("unused") 683cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onAttachFragment(Fragment fragment) { 684cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 685d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 686cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 687cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the FragmentManager for interacting with fragments associated 688cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * with this activity. 689cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 690cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManager getSupportFragmentManager() { 691d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return mFragments.getSupportFragmentManager(); 692d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 693d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 694d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy public LoaderManager getSupportLoaderManager() { 695d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return mFragments.getSupportLoaderManager(); 696cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 697cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 698cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 699cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Modifies the standard behavior to allow results to be delivered to fragments. 700cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This imposes a restriction that requestCode be <= 0xffff. 701cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 702cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 703cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void startActivityForResult(Intent intent, int requestCode) { 704ba02c82a9486ed9b9ccfd3fa7fcf02f54c07830cDaniel Resnick // If this was started from a Fragment we've already checked the upper 16 bits were not in 705ba02c82a9486ed9b9ccfd3fa7fcf02f54c07830cDaniel Resnick // use, and then repurposed them for the Fragment's index. 706ba02c82a9486ed9b9ccfd3fa7fcf02f54c07830cDaniel Resnick if (!mStartedActivityFromFragment) { 707fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri if (requestCode != -1) { 708fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri checkForValidRequestCode(requestCode); 7090e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick } 710cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 711cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super.startActivityForResult(intent, requestCode); 712cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 713cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 714e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav @Override 715e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav public final void validateRequestPermissionsRequestCode(int requestCode) { 71686f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell // We use 16 bits of the request code to encode the fragment id when 717e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav // requesting permissions from a fragment. Hence, requestPermissions() 718e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav // should validate the code against that but we cannot override it as 719e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav // we can not then call super and also the ActivityCompat would call 720e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav // back to this override. To handle this we use dependency inversion 721e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav // where we are the validator of request codes when requesting 722e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav // permissions in ActivityCompat. 72386f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell if (!mRequestedPermissionsFromFragment 724fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri && requestCode != -1) { 725fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri checkForValidRequestCode(requestCode); 726e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 727e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 728e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav 729e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav /** 730e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * Callback for the result from requesting permissions. This method 731e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * is invoked for every call on {@link #requestPermissions(String[], int)}. 732dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov * <p> 733dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov * <strong>Note:</strong> It is possible that the permissions request interaction 734dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov * with the user is interrupted. In this case you will receive empty permissions 735dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov * and results arrays which should be treated as a cancellation. 736dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov * </p> 737e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * 738e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. 739e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * @param permissions The requested permissions. Never null. 740e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * @param grantResults The grant results for the corresponding permissions 741e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED} 742e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null. 743e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * 744e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * @see #requestPermissions(String[], int) 745e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav */ 74690ac236504c1a4cac7e91f1ffc523334f2a8f399Aurimas Liutikas @Override 747e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 748e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav @NonNull int[] grantResults) { 749fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri int index = (requestCode >> 16) & 0xffff; 750e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav if (index != 0) { 751e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav index--; 75286f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell 75386f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell String who = mPendingFragmentActivityResults.get(index); 75486f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell mPendingFragmentActivityResults.remove(index); 75586f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell if (who == null) { 75686f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell Log.w(TAG, "Activity result delivered for unknown Fragment."); 757e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav return; 758e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 75986f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell Fragment frag = mFragments.findFragmentByWho(who); 760e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav if (frag == null) { 76186f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell Log.w(TAG, "Activity result no fragment exists for who: " + who); 762e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } else { 763fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri frag.onRequestPermissionsResult(requestCode & 0xffff, permissions, grantResults); 764e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 765e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 766e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 767e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav 768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Called by Fragment.startActivityForResult() to implement its behavior. 770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 771d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy public void startActivityFromFragment(Fragment fragment, Intent intent, 772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int requestCode) { 7730e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick startActivityFromFragment(fragment, intent, requestCode, null); 7740e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick } 7750e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick 7760e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick /** 7770e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick * Called by Fragment.startActivityForResult() to implement its behavior. 7780e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick */ 7790e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick public void startActivityFromFragment(Fragment fragment, Intent intent, 7800e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick int requestCode, @Nullable Bundle options) { 7810e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick mStartedActivityFromFragment = true; 7820e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick try { 7830e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick if (requestCode == -1) { 7840e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick ActivityCompat.startActivityForResult(this, intent, -1, options); 7850e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick return; 7860e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick } 787fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri checkForValidRequestCode(requestCode); 788a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick int requestIndex = allocateRequestIndex(fragment); 7890e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick ActivityCompat.startActivityForResult( 790fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri this, intent, ((requestIndex + 1) << 16) + (requestCode & 0xffff), options); 7910e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick } finally { 7920e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick mStartedActivityFromFragment = false; 793cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 794cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 795d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 796fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri /** 797fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri * Called by Fragment.startIntentSenderForResult() to implement its behavior. 798fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri */ 799fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri public void startIntentSenderFromFragment(Fragment fragment, IntentSender intent, 800fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, 801fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri int extraFlags, Bundle options) throws IntentSender.SendIntentException { 802fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri mStartedIntentSenderFromFragment = true; 803fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri try { 804fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri if (requestCode == -1) { 805fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri ActivityCompat.startIntentSenderForResult(this, intent, requestCode, fillInIntent, 806fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri flagsMask, flagsValues, extraFlags, options); 807fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri return; 808fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri } 809fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri checkForValidRequestCode(requestCode); 810fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri int requestIndex = allocateRequestIndex(fragment); 811fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri ActivityCompat.startIntentSenderForResult(this, intent, 812fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri ((requestIndex + 1) << 16) + (requestCode & 0xffff), fillInIntent, 813fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri flagsMask, flagsValues, extraFlags, options); 814fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri } finally { 815fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri mStartedIntentSenderFromFragment = false; 816fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri } 817fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri } 818fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri 819a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // Allocates the next available startActivityForResult request index. 820a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick private int allocateRequestIndex(Fragment fragment) { 821a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // Sanity check that we havn't exhaused the request index space. 822a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick if (mPendingFragmentActivityResults.size() >= MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS) { 823a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick throw new IllegalStateException("Too many pending Fragment activity results."); 824a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 825a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 826a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick // Find an unallocated request index in the mPendingFragmentActivityResults map. 827a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick while (mPendingFragmentActivityResults.indexOfKey(mNextCandidateRequestIndex) >= 0) { 828a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mNextCandidateRequestIndex = 829a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick (mNextCandidateRequestIndex + 1) % MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS; 830a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 831a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 832a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick int requestIndex = mNextCandidateRequestIndex; 833a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mPendingFragmentActivityResults.put(requestIndex, fragment.mWho); 834a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick mNextCandidateRequestIndex = 835a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick (mNextCandidateRequestIndex + 1) % MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS; 836a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 837a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick return requestIndex; 838a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick } 839a76a4339be2f27431a167381187c3f0b0d08f1b7Daniel Resnick 840e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav /** 841e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav * Called by Fragment.requestPermissions() to implement its behavior. 842e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav */ 843dea7d510a375df0e0e703bfba5ff63c8acca849eAurimas Liutikas void requestPermissionsFromFragment(Fragment fragment, String[] permissions, 844e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav int requestCode) { 845e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav if (requestCode == -1) { 846e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav ActivityCompat.requestPermissions(this, permissions, requestCode); 847e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav return; 848e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 849fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri checkForValidRequestCode(requestCode); 85086f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell try { 85186f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell mRequestedPermissionsFromFragment = true; 85286f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell int requestIndex = allocateRequestIndex(fragment); 85386f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell ActivityCompat.requestPermissions(this, permissions, 85486f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell ((requestIndex + 1) << 16) + (requestCode & 0xffff)); 85586f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell } finally { 85686f3b80ddf7f9aa5c5b7afe77217cb75632d62a2Adam Powell mRequestedPermissionsFromFragment = false; 857e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 858e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 859e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav 8608491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy class HostCallbacks extends FragmentHostCallback<FragmentActivity> { 861d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy public HostCallbacks() { 862d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy super(FragmentActivity.this /*fragmentActivity*/); 863cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 864d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 865d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 8668491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 867d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy FragmentActivity.this.dump(prefix, fd, writer, args); 868cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 869d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 870d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 8718491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public boolean onShouldSaveFragmentState(Fragment fragment) { 872d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return !isFinishing(); 873d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 874d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 875d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 8768491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public LayoutInflater onGetLayoutInflater() { 877d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return FragmentActivity.this.getLayoutInflater().cloneInContext(FragmentActivity.this); 878d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 879d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 880d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 8818491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public FragmentActivity onGetHost() { 882edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy return FragmentActivity.this; 883edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy } 884edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy 885edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy @Override 8868491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public void onSupportInvalidateOptionsMenu() { 887d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy FragmentActivity.this.supportInvalidateOptionsMenu(); 888d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 889d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 890d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 8918491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode) { 892d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy FragmentActivity.this.startActivityFromFragment(fragment, intent, requestCode); 893d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 894d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 895d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 8960e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick public void onStartActivityFromFragment( 8970e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick Fragment fragment, Intent intent, int requestCode, @Nullable Bundle options) { 8980e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick FragmentActivity.this.startActivityFromFragment(fragment, intent, requestCode, options); 8990e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick } 9000e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick 9010e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick @Override 902fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri public void onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent, 903fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, 904fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri int extraFlags, Bundle options) throws IntentSender.SendIntentException { 905fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri FragmentActivity.this.startIntentSenderFromFragment(fragment, intent, requestCode, 906fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri fillInIntent, flagsMask, flagsValues, extraFlags, options); 907fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri } 908fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri 909fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri @Override 910e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav public void onRequestPermissionsFromFragment(@NonNull Fragment fragment, 911e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav @NonNull String[] permissions, int requestCode) { 912e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav FragmentActivity.this.requestPermissionsFromFragment(fragment, permissions, 913e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav requestCode); 914e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 915e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav 916e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav @Override 917e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav public boolean onShouldShowRequestPermissionRationale(@NonNull String permission) { 918e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav return ActivityCompat.shouldShowRequestPermissionRationale( 919e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav FragmentActivity.this, permission); 920e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav } 921e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav 922e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav @Override 9238491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public boolean onHasWindowAnimations() { 924d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return getWindow() != null; 925d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 926d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 927d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 9288491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public int onGetWindowAnimations() { 929d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy final Window w = getWindow(); 930d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return (w == null) ? 0 : w.getAttributes().windowAnimations; 931d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 932d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 933b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy @Override 934b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy public void onAttachFragment(Fragment fragment) { 935b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy FragmentActivity.this.onAttachFragment(fragment); 936b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy } 937b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy 938d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Nullable 939d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 9408491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public View onFindViewById(int id) { 941d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return FragmentActivity.this.findViewById(id); 942d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy } 943d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 944d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @Override 9458491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public boolean onHasView() { 946d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy final Window w = getWindow(); 947d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return (w != null && w.peekDecorView() != null); 948cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 949cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 950cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 951