@高清馬里奧 onActivityResult沒(méi)有問(wèn)題是因?yàn)樗恢С纸獬卣{(diào), 所以它回調(diào)一直存在;
ActivtiyResult標(biāo)準(zhǔn)寫法在銷毀重建過(guò)程中onDestroy也是會(huì)解除注冊(cè)的
LifecycleEventObserver observer = new LifecycleEventObserver() {
@Override
@SuppressWarnings("deprecation")
public void onStateChanged(
@NonNull LifecycleOwner lifecycleOwner,
@NonNull Lifecycle.Event event) {
if (Lifecycle.Event.ON_START.equals(event)) {
mKeyToCallback.put(key, new CallbackAndContract<>(callback, contract));
if (mParsedPendingResults.containsKey(key)) {
@SuppressWarnings("unchecked")
final O parsedPendingResult = (O) mParsedPendingResults.get(key);
mParsedPendingResults.remove(key);
callback.onActivityResult(parsedPendingResult);
}
final ActivityResult pendingResult = mPendingResults.getParcelable(key);
if (pendingResult != null) {
mPendingResults.remove(key);
callback.onActivityResult(contract.parseResult(
pendingResult.getResultCode(),
pendingResult.getData()));
}
} else if (Lifecycle.Event.ON_STOP.equals(event)) {
mKeyToCallback.remove(key);
} else if (Lifecycle.Event.ON_DESTROY.equals(event)) {
unregister(key);
}
}
};
只不過(guò)它在onCreate時(shí)又重新注冊(cè)了, 你在使用即時(shí)注冊(cè)時(shí)并不會(huì)在onCreate中重新注冊(cè), 這就是問(wèn)題的根源, 所以我說(shuō)這是邏輯問(wèn)題.
Android Result Api不能在生命周期onStart及之后注冊(cè)的解決辦法現(xiàn)在Activity的startActivityForResult廢棄了,Google建議我們使用Activity Result Api. 然而這個(gè)Activity Resu...