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

Kevin Hester

02/09/2020, 2:30 PM
Hi ya'll I've been using kotlin on android for a while but I'm only now trying Jetpack Compose. I've read through the tutorials but I don't understand one thing: how do I convert changes from outside of the Compose hierarchy into Model visible changes that cause the GUI to reactively redraw? My use case is "I have a BroadcastReceiver that is setting a bool called isConnected to true". Since this bool is not changing due to a button press (or some other event GUI widget), it is starting outside of "compose land 😉 ". I tried wrapping the bool with +state but that throws an error on the line of declaration "java.lang.IllegalStateException: Composition requires an active composition context" Any advice appreciated!
m

Mark Murphy

02/09/2020, 2:34 PM
FYI, there is a #compose channel dedicated to Jetpack Compose questions. You may have more luck there, particularly if you can provide some code snippets (e.g., where is this receiver?).
2
a

Adam Powell

02/09/2020, 2:35 PM
If you change an
@Model
property or
MutableState<T>
outside of composition, you're already good to go and compose can see it. For example, if you did nothing more than declare that
isConnected
as
Copy code
var isConnected by mutableStateOf(false)
you're already set.
And yes, come join us over in #compose too 😃
Update to -dev04, there are instructions over in #compose in the pinned messages. We've dropped the leading + from effect calls and added
mutableStateOf()
that can be called outside of composition
❤️ 1
k

Kevin Hester

02/09/2020, 2:47 PM
ooh! That is super helpful! I'll also move on over to compose.
a

Adam Powell

02/09/2020, 3:12 PM
also something that takes a bit to get used to - that
isConnected
state doesn't have to be read directly by compose for compose to see the change. If anything in the call stack accesses it during composition, compose will record the dependency on it. It can be a private property several objects away.
k

Kevin Hester

02/09/2020, 6:28 PM
thanks! It reminds me of how Meteor tracks changes also. btw - your mutableStateOf feature worked great for me.
👍 1
2 Views