From 52f32c5b14632a991789785d94e45d035ad7b4ee Mon Sep 17 00:00:00 2001 From: Nishan Wijesinghe Date: Wed, 22 May 2024 15:19:59 +0200 Subject: [PATCH] Commit CoperFragment only before onStateSaved (#44) --- .../main/java/com/vinted/coper/CoperImpl.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/vinted/coper/CoperImpl.kt b/library/src/main/java/com/vinted/coper/CoperImpl.kt index 50dae99..f8a1309 100644 --- a/library/src/main/java/com/vinted/coper/CoperImpl.kt +++ b/library/src/main/java/com/vinted/coper/CoperImpl.kt @@ -106,13 +106,17 @@ internal class CoperImpl( fragmentManager: FragmentManager ): CoperFragment { return suspendCancellableCoroutine { continuation -> - fragmentManager.beginTransaction() - .add(this, FRAGMENT_TAG) - .runOnCommit { - continuation.resume(this) - } - .commit() - _latestCommittedFragmentFlow.value = this + if (!fragmentManager.isStateSaved) { + fragmentManager.beginTransaction() + .add(this, FRAGMENT_TAG) + .runOnCommit { + continuation.resume(this) + } + .commit() + _latestCommittedFragmentFlow.value = this + } else { + continuation.cancel() + } } }