@高清马里奥 onActivityResult没有问题是因为它不支持解除回调, 所以它回调一直存在;
ActivtiyResult标准写法在销毁重建过程中onDestroy也是会解除注册的
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);
}
}
};
只不过它在onCreate时又重新注册了, 你在使用即时注册时并不会在onCreate中重新注册, 这就是问题的根源, 所以我说这是逻辑问题.
Android Result Api不能在生命周期onStart及之后注册的解决办法现在Activity的startActivityForResult废弃了,Google建议我们使用Activity Result Api. 然而这个Activity Resu...