diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationDetailFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationDetailFragment.kt index 73db8a9d40d..fda1dcb8256 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationDetailFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationDetailFragment.kt @@ -261,6 +261,7 @@ class VariationDetailFragment : } } + @Suppress("LongMethod") private fun setupObservers(viewModel: VariationDetailViewModel) { viewModel.variationViewStateData.observe(viewLifecycleOwner) { old, new -> new.variation.takeIfNotEqualTo(old?.variation) { newVariation -> @@ -318,8 +319,13 @@ class VariationDetailFragment : is ExitWithResult<*> -> navigateBackWithResult(KEY_VARIATION_DETAILS_RESULT, event.data) is ShowDialog -> event.showDialog() is ShowDialogFragment -> event.showIn(parentFragmentManager, this) - is Exit -> requireActivity().onBackPressedDispatcher.onBackPressed() is VariationDetailViewModel.ShowUpdateVariationError -> showUpdateVariationError(event.message) + is Exit -> { + // Ensure subsequent Exit events are ignored to avoid IllegalStateException + viewModel.event.removeObservers(viewLifecycleOwner) + requireActivity().onBackPressedDispatcher.onBackPressed() + } + else -> event.isHandled = false } } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationsBulkUpdateBaseFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationsBulkUpdateBaseFragment.kt index 806471f3dbc..49b4f56d812 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationsBulkUpdateBaseFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/variations/VariationsBulkUpdateBaseFragment.kt @@ -53,6 +53,7 @@ abstract class VariationsBulkUpdateBaseFragment(@LayoutRes layoutId: Int) : Base viewModel.onDoneClicked() true } + else -> false } } @@ -68,7 +69,12 @@ abstract class VariationsBulkUpdateBaseFragment(@LayoutRes layoutId: Int) : Base ActivityUtils.hideKeyboard(requireActivity()) uiMessageResolver.showSnack(event.message) } - is MultiLiveEvent.Event.Exit -> requireActivity().onBackPressedDispatcher.onBackPressed() + + is MultiLiveEvent.Event.Exit -> { + // Ensure subsequent Exit events are ignored to avoid IllegalStateException + viewModel.event.removeObservers(viewLifecycleOwner) + requireActivity().onBackPressedDispatcher.onBackPressed() + } } } }