Assigned
Status Update
Comments
ti...@google.com <ti...@google.com> #2
We could add an API to make the weighted distance customizable, aosp/3271691 has more info.
Bo...@T-Mobile.com <Bo...@T-Mobile.com> #3
Thank you for your response.
Here, I mean that the "onResumeCallback" method passed to AttachLifeCycleEvent() function is getting called multiple times in both background and foreground mode.
Regarding logs,
We understand that it is very tricky to track this without logs. But, as I mentioned in the observation, we don't have any UI logs and we observed it through server's splunk logs.
Question:
We are puzzled so much, to understand why is this LifecycleEventObserver triggering "Lifecycle.Event.ON_RESUME" events multiple times even when the app is in background in jetpack compose? Can you please check my AttachLifeCycleEvent() method once?
Here, I mean that the "onResumeCallback" method passed to AttachLifeCycleEvent() function is getting called multiple times in both background and foreground mode.
Regarding logs,
We understand that it is very tricky to track this without logs. But, as I mentioned in the observation, we don't have any UI logs and we observed it through server's splunk logs.
Question:
We are puzzled so much, to understand why is this LifecycleEventObserver triggering "Lifecycle.Event.ON_RESUME" events multiple times even when the app is in background in jetpack compose? Can you please check my AttachLifeCycleEvent() method once?
jo...@google.com <jo...@google.com> #4
CC Ian, maybe you have an idea?
Description
Jetpack Compose component used: DisposableEffect & LocalLifecycleOwner
Android Studio Build:
Android Studio Electric Eel | 2022.1.1 RC 3
Build #AI-221.6008.13.2211.9424903, built on December 20, 2022
Runtime version: 11.0.15+0-b2043.56-8887301 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 13.2
Kotlin version:1.7.21
Device: samsung SM-N960U1, samsung SM-N950U
OS Version: 10, 9
Steps to Reproduce or Code Sample to Reproduce:
1. No specific steps and happening randomly at production Environment
Code Samples:
==============
The intention is to perform the refresh call whenever screen renders first time or onResume or comes from background to foreground at Dashboard page
Here is the reusable function to detect the ON_RESUME events
@Composable
fun AttachLifeCycleEvent(
onCreateCallback: (() -> Unit)? = null, onResumeCallback: (() -> Unit)? = null
) {
val lifeCycleOwner = LocalLifecycleOwner.current
val scope = rememberCoroutineScope()
// If `lifecycleOwner` changes, dispose and reset the effect
DisposableEffect(lifeCycleOwner) {
val eventObserver = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_PAUSE -> {
Timber.d("ON_PAUSE")
}
Lifecycle.Event.ON_RESUME -> {
Timber.d("ON_REUME")
scope.launch {
onResumeCallback?.invoke()
}
}
Lifecycle.Event.ON_CREATE -> {
Timber.d("OnCreate")
onCreateCallback?.invoke()
}
else -> {
Timber.d("Else branch of $event")
}
}
}
Timber.d("Add Observer")
// Add the observer to the lifecycle
lifeCycleOwner.lifecycle.addObserver(eventObserver)
// When the effect leaves the Composition, remove the observer
onDispose {
Timber.d("Removed Observer")
lifeCycleOwner.lifecycle.removeObserver(eventObserver)
}
}
}
Usage at Dashboard screen:
===================
@Composable
fun DashboardScreen(
dashboardViewModel: DashboardViewModel
) {
AttachLifeCycleEvent(onResumeCallback = {
// This block is calling multiple times in loop both foreground & background mode
dashboardViewModel.refresh()
})
}
Observation:
===========
In PROD, It did worked 99.9% of devices as we expected, but some devices and some edge cases , refresh call goes in LOOP and we couldn't reproduce this issue at DEV environment. But, we do detect this with server LOGS.
Stack trace (if applicable): N/A