Bundle result) { if (result == null) { // if the given result is null, remove the result mResults.remove(requestKey); return; } // Check if there is a listener waiting for a result with this key LifecycleAwareResultListener resultListener = mResultListeners.get(requestKey); // if there is and it is sta rt ed, fi re the callback if (resultListener != null && resultListener.isAtLeast(Lifecycle.State.STARTED)) { resultListener.onFragmentResult(requestKey, result); } else { // else, save the result for later mResults.put(requestKey, result); } } @SuppressLint("SyntheticAccessor") @Override public fi nal void setResultListener(@NonNull fi nal String requestKey, @NonNull fi nal LifecycleOwner lifecycleOwner, @Nullable fi nal FragmentResultListener listener) { if (listener == null) { mResultListeners.remove(requestKey); return; } fi nal Lifecycle lifecycle = lifecycleOwner.getLifecycle(); if (lifecycle.getCurrentState() == Lifecycle.State.DESTROYED) { return; } LifecycleEventObserver observer = new LifecycleEventObserver() { @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) { if (event == Lifecycle.Event.ON_START) { Bundle storedResult = mResults.get(requestKey); if (storedResult != null) { listener.onFragmentResult(requestKey, storedResult); setResult(requestKey, null); } } if (event == Lifecycle.Event.ON_DESTROY) { lifecycle.removeObserver(this); mResultListeners.remove(requestKey); } } }; lifecycle.addObserver(observer); mResultListeners.put(requestKey, new LifecycleAwareResultListener(lifecycle, listener)); }