mbonnin
02/23/2020, 4:22 PMComposable
to an Activity lifecycle ? With Views
, I could hold a reference to the View and call a method from onDestroy
but I'm not sure how to do with compose ?
override fun onDestroy() {
super.onDestroy()
// How do I get a reference to a composable or how do I dispose my composable ?
composable.dispose()
}
Mark Murphy
02/23/2020, 4:24 PMmbonnin
02/23/2020, 4:27 PMmbonnin
02/23/2020, 4:28 PMAdam Powell
02/23/2020, 4:34 PMonCommit { onDispose {} }
you can see the general pattern. You'll want to listen to both the composition commit/dispose pair and the lifecycle callbacks combinedAdam Powell
02/23/2020, 4:37 PMsuspend
bugs are taken care of with our IR compiler setup there will probably be some variant that just gives you a suspending block that cancels on dispose, you can collect {}
flows directly in, etc.mbonnin
02/23/2020, 4:38 PMAdam Powell
02/23/2020, 4:38 PMAdam Powell
02/23/2020, 4:39 PMonCommit(observable) {
val observer = makeObserver()
val disposable = observable.subscribe(observer)
onDispose {
disposable.dispose()
}
}
mbonnin
02/23/2020, 4:39 PMAdam Powell
02/23/2020, 4:40 PMonCommit
is important; if it changes from composition to composition, the old scope will have its onDispose
called and a new one will run. If it's equal from composition to composition, it's left alone.mbonnin
02/23/2020, 4:41 PMonDestroy
to finish composition ? Or is that taken care of automagically ?Adam Powell
02/23/2020, 4:43 PMmbonnin
02/23/2020, 4:44 PMmbonnin
02/23/2020, 4:45 PMAdam Powell
02/23/2020, 4:45 PMActivity.setContent
extension should set all of that up for youAdam Powell
02/23/2020, 4:45 PMLeland Richardson [G]
02/23/2020, 4:48 PMLeland Richardson [G]
02/23/2020, 4:48 PMdisposeComposition
to get this behaviorMark Murphy
02/23/2020, 4:51 PMmy toplevel compose function will register a timer to update the timeTo me, this feels like "my compose function will call a Web service". Is that really the architecturally-correct way to do this sort of thing in Compose?
Adam Powell
02/23/2020, 4:59 PMAdam Powell
02/23/2020, 5:00 PMAdam Powell
02/23/2020, 5:01 PMMark Murphy
02/23/2020, 5:05 PM@Composable
private fun CountingButton() {
Padding(8.dp) {
val count = state { 0 }
Button(
text = "You clicked the button ${count.value} times!",
onClick = { count.value += 1 }
)
}
}
and having the composable call a Web service. These state
examples make things simple, but I'm still struggling with the dividing line between that and when you need something more elaborate.
Going back to Martin's timer, the objective of making the timer be self-contained is wonderful. And doing it all within a pure composable should be doable. I'm just still wrapping my head around whether that's the right solution for something small-ish like a timer.Adam Powell
02/23/2020, 5:11 PMAdam Powell
02/23/2020, 5:13 PMCountingButton
into a parameter with a default - testability and anything outside of the button observing, saving or restoring the current count.Adam Powell
02/23/2020, 5:13 PMAdam Powell
02/23/2020, 5:15 PMstate {}
but ties into the most-local ViewModelStoreOwner
, e.g. val counter = viewModel<CounterViewModel> { ... }
Adam Powell
02/23/2020, 5:16 PMMark Murphy
02/23/2020, 5:19 PMAdam Powell
02/23/2020, 5:21 PMAdam Powell
02/23/2020, 5:26 PMAdam Powell
02/23/2020, 5:28 PMAdam Powell
02/23/2020, 5:28 PMmbonnin
02/24/2020, 5:06 PMonCommit/onDispose
apis. It seems to work well but calling disposeComposable()
from onDestroy
doesn't seem to dispose anything. Calling setContent { Container { }
instead did the job though.Manuel Wrage
02/24/2020, 9:32 PMLeland Richardson [G]
02/24/2020, 9:46 PMLeland Richardson [G]
02/24/2020, 9:47 PM