是否将私有副本Bundle
传递给Fragment
父级Activity
?
我尝试在 Activity 中保存键、值对onSaveInstanceState
并Fragment
尝试在onCreate
Activity 中检索它。它在那里不存在。
但两者之间似乎也有联系。当我null
传给super.onCreate
的传给Activity
的也是。Bundle
Fragment
onCreate
null
是否将私有副本Bundle
传递给Fragment
父级Activity
?
我尝试在 Activity 中保存键、值对onSaveInstanceState
并Fragment
尝试在onCreate
Activity 中检索它。它在那里不存在。
但两者之间似乎也有联系。当我null
传给super.onCreate
的传给Activity
的也是。Bundle
Fragment
onCreate
null
Bundle
它被发送到onCreate
并且Activity
完全Fragment
不同。如果您要发送null
到super.onCreate
-> 活动,将fragments
从头开始重新创建所有内容。所以你Fragment
也会收到null
->。因为这是新的fragment
这是代码的一部分FragmentActivity
:
/**
* Perform initialization of all fragments and loaders.
*/
@SuppressWarnings("deprecation")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mFragments.attachHost(null /*parent*/);
super.onCreate(savedInstanceState);
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
mFragments.restoreLoaderNonConfig(nc.loaders);
}
if (savedInstanceState != null) {
Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
mFragments.restoreAllState(p, nc != null ? nc.fragments : null);
// Check if there are any pending onActivityResult calls to descendent Fragments.
if (savedInstanceState.containsKey(NEXT_CANDIDATE_REQUEST_INDEX_TAG)) {
mNextCandidateRequestIndex =
savedInstanceState.getInt(NEXT_CANDIDATE_REQUEST_INDEX_TAG);
int[] requestCodes = savedInstanceState.getIntArray(ALLOCATED_REQUEST_INDICIES_TAG);
String[] fragmentWhos = savedInstanceState.getStringArray(REQUEST_FRAGMENT_WHO_TAG);
if (requestCodes == null || fragmentWhos == null ||
requestCodes.length != fragmentWhos.length) {
Log.w(TAG, "Invalid requestCode mapping in savedInstanceState.");
} else {
mPendingFragmentActivityResults = new SparseArrayCompat<>(requestCodes.length);
for (int i = 0; i < requestCodes.length; i++) {
mPendingFragmentActivityResults.put(requestCodes[i], fragmentWhos[i]);
}
}
}
}
if (mPendingFragmentActivityResults == null) {
mPendingFragmentActivityResults = new SparseArrayCompat<>();
mNextCandidateRequestIndex = 0;
}
mFragments.dispatchCreate();
}