https://kotlinlang.org logo
#compose
Title
# compose
k

Kevin Hester

02/13/2020, 3:05 AM
hi ya'll! I'm a compose noob (but a fair amount of kotlin experience) who is trying to convert my app over to compose - partly as an experiment and partly because I love reactive frameworks. A question: What is the best "compose way" to run code when the view changes? i.e. based on changes to a "screen" model variable it nicely changes which content gets generated. But when I change to my "bluetooth scan screen" I need to start the android BTLE scan operation (with a callback that gets invoked to add elements to my model "list of bluetooth devices"). When that screen goes away (because the screen variable got changed). I need to run some code to stop and destroy the BLE scanner class.
Copy code
when (screen) {
    is Screen.Home -> HomeContent()
    is Screen.SelectRadio -> BTScanScreen()
    // Question: how to get hooks invoked when this screen gets shown/removed?
    // i.e. I need to start/stop a bluetooth scan operation. depending on the
    // appearance/disappearance of this screen.
}
a

Adam Powell

02/13/2020, 3:13 AM
k

Kevin Hester

02/13/2020, 3:01 PM
OOH SUPER COOL - that is perfect. Thanks so much!
👍 2
a

Adam Powell

02/13/2020, 3:07 PM
do note that these are lifecycle of the composition, not related to activity lifecycle. If you want things to stop as part of
onStop
in the
LifecycleObserver
sense, you'll need to combine those two things together. Some out of the box API will be around for this later
👍 1
k

Kevin Hester

02/13/2020, 3:33 PM
thanks for that extra note. You're awesome (and great framework you've made). I think onActive with its onDispose callback is perfect for my case. After I have it working and cleaned up a bit, I'll send the .kt to you. Feel free to use it if you want to provide a small self contained demo of how to make Compose work with sideeffectish low level android services.
2 Views