Skip to content

Commit

Permalink
Merge pull request #13281 from woocommerce/issue/13226-fix-crash-on-exit
Browse files Browse the repository at this point in the history
Fix crash when Exit event is fired multiple times
  • Loading branch information
JorgeMucientes authored Jan 13, 2025
2 parents 3a1eec1 + 792072c commit fe05fe6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class VariationsBulkUpdateBaseFragment(@LayoutRes layoutId: Int) : Base
viewModel.onDoneClicked()
true
}

else -> false
}
}
Expand All @@ -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()
}
}
}
}
Expand Down

0 comments on commit fe05fe6

Please sign in to comment.