'Kotlin Variable changes are not applied in method and observer
private var enableBottomSheet = false
bottomSheetStack.observe(requireActivity()) { stack ->
logd("========= enableBottomSheet: ${enableBottomSheet}")
showBottomSheet(stack)
}
override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible)
enableBottomSheet = menuVisible
lifecycleScope.launch{
repeat(100){
logd("==== TIME($it): enableBottomSheet $enableBottomSheet")
delay(100L)
}
}
if (menuVisible) {
viewModel.apiCall()
}else{
bottomSheetJob?.cancel()
}
}
private fun showBottomSheet(stack: Stack<BottomSheetStatusModel>) {
logd("====== http showBottomSheet enableBottomSheet: ${enableBottomSheet}")
if(!enableBottomSheet) return
if (stack.size > 0) {
try {
bottomSheetJob = lifecycleScope.launch {
delay(100L)
try {
findNavController().navigate(R.id.action_mainContainerFragment_to_statusBottomSheet, bundle, null)
}catch (e: java.lang.Exception){
e.printStackTrace()
}
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
}
And I got this result:
================ http setMenuVisibility enableBottomSheet: true ================
==== TIME(0): enableBottomSheet true
...
========= http enableBottomSheet: false
==== TIME(13): enableBottomSheet true
...
==== TIME(22): enableBottomSheet true
====== http showBottomSheet enableBottomSheet: false
...
==== TIME(99): enableBottomSheet true
There is no part that assigns false value to the enableBottomSheet.
I don't know why enableBottomSheet keeps getting false.
It happens when I go like A_Fragment(A1_Fragment) -> B_Fragment -> A_Fragment(A1_Fragment) -> A_Fragment(A2_Fragment uses the code above).
(A_Fragment is the parent fragment that contains A1~A2_Fragment as ViewPager2. B_Fragment is a fragment that can go from A1_Fragment.)
Why is it happened?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
