Skip to content

Attempt to fix crash on ProductListToolbarHelper #11484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*** PLEASE FOLLOW THIS FORMAT: [<priority indicator, more stars = higher priority>] <description> [<PR URL>]
18.7
-----

- [*] Products: Fixed a bug that caused the app to crash sometimes during screen rotation [https://github.com/woocommerce/woocommerce-android/pull/11484]

18.6
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class ProductListToolbarHelper @Inject constructor(
private var scanBarcodeMenuItem: MenuItem? = null
private var searchView: SearchView? = null

private val refreshOptionsMenuCallback = Runnable {
refreshOptionsMenu()
}

override fun onCreate(owner: LifecycleOwner) {
(activity as FragmentActivity).onBackPressedDispatcher.addCallback(
owner,
Expand Down Expand Up @@ -86,7 +90,7 @@ class ProductListToolbarHelper @Inject constructor(
this.viewModel = productListViewModel
this.binding = binding

fragment.lifecycle.addObserver(this)
fragment.viewLifecycleOwner.lifecycle.addObserver(this)
Copy link
Member Author

@hichamboushaba hichamboushaba May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change also ensures that we don't leak the fragment's view, we were setting binding to null in the fragment's onDestroy due to tying it to its Lifecycle, but now it'll be executed correctly on a matching callback of onDestroyView.


if (productListViewModel.isSearching()) {
binding.productsSearchTabView.isVisible = true
Expand All @@ -98,6 +102,7 @@ class ProductListToolbarHelper @Inject constructor(

override fun onDestroy(owner: LifecycleOwner) {
disableSearchListeners()
binding?.toolbar?.removeCallbacks(refreshOptionsMenuCallback)
listFragment = null
searchMenuItem = null
scanBarcodeMenuItem = null
Expand Down Expand Up @@ -169,9 +174,7 @@ class ProductListToolbarHelper @Inject constructor(

// We want to refresh the options menu after the toolbar has been inflated
// Otherwise, logic in it will be executed before the toolbar is in restored state after configuration change
toolbar.post {
refreshOptionsMenu()
}
toolbar.post(refreshOptionsMenuCallback)
}

private fun refreshOptionsMenu() {
Expand Down
Loading