简体   繁体   中英

Accessing composable function from within non-composable function

Is there any way, using Android Jetpack Compose, to access a composable function or an ambientOf value from within a non-composable function?

For instance, I have a val in MainActivity that I would like to access/modify from within a non-composable function. Is this possible? If so, how do I go about doing this?

Your mental model of composable functions isn't quite right here. Calling a composable function from within a non-composable function doesn't make sense.

For your specific example of ambientOf , the ambient value doesn't exist outside of composition—you can think of an ambient as being supplied to everything "below" it in the composition tree. Accessing it outside of that tree doesn't work: you can't read it because it doesn't exist outside of the tree (it would always have no value even if you could), and you can't write to it because there's no corresponding location in the tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM