ursus
03/17/2021, 1:43 PMActivity.onStop
?Zach Klippenstein (he/him) [MOD]
03/17/2021, 2:10 PMComposition
is disposed, so the answer depends on your ViewCompositionStrategy – the default will dispose the composition when the view is detached, but there’s another strategy that waits until the lifecycle is destroyed.
Edit: added “for good” – the dispatcher will pause in certain lifecycle states, which effectively stops rendering, see thread below.ursus
03/17/2021, 2:13 PMAdam Powell
03/17/2021, 2:13 PMAdam Powell
03/17/2021, 2:14 PMursus
03/17/2021, 2:15 PMAdam Powell
03/17/2021, 2:15 PMwithFrameNanos
API; the underlying MonotonicFrameClock
pauses while the lifecycle is stopped.Adam Powell
03/17/2021, 2:15 PMAdam Powell
03/17/2021, 2:16 PMAdam Powell
03/17/2021, 2:17 PMursus
03/17/2021, 2:18 PMZach Klippenstein (he/him) [MOD]
03/17/2021, 2:23 PMif viewmodel state changed while in background, view wont get rerenderedI think so, based on what Adam is saying. IIUC because the dispatcher is paused, if you’re using `Flow`s, the flow will effectively experience backpressure when you emit while collector is paused. So the “correct” behavior could depend on your
ViewModel
implementation – if the subscription represents some expensive resource, it might make more sense to unsubscribe/stop collecting entirely when the view won’t be updated. But if it’s just backed by something cheap like a MutableStateFlow
then allowing the subscription to persist and just not update the view might be ok.Adam Powell
03/17/2021, 2:25 PMAdam Powell
03/17/2021, 2:26 PM.flowWithLifecycle
, which will stop and restart collection of an upstream flow while a lifecycle is below a given state, and repeatOnLifecycle
, which can be used inside LaunchedEffect or elsewhereZach Klippenstein (he/him) [MOD]
03/17/2021, 2:27 PMursus
03/17/2021, 2:28 PMAdam Powell
03/17/2021, 2:29 PMZach Klippenstein (he/him) [MOD]
03/17/2021, 2:30 PMThese APIs are meant to replace the pausing coroutine dispatcher in lifecycle-ktx,Ah, I thought they were built on top of this dispatcher. Thank god, that thing creeped me out.
Adam Powell
03/17/2021, 2:30 PMursus
03/17/2021, 2:31 PMAdam Powell
03/17/2021, 2:32 PMAdam Powell
03/17/2021, 2:35 PMursus
03/17/2021, 2:36 PMursus
03/17/2021, 2:38 PMappInForeground
.flatMapLatest {
if (!it) emptyFlow() else doStuff()
}
this pattern should get promoted more, really handyAdam Powell
03/17/2021, 2:41 PMflowOnLifecycle
operator does 🙂Adam Powell
03/17/2021, 2:41 PM