Chris Fillmore
05/31/2021, 7:58 PMclass MyViewModel : ViewModel() {
val plugin: Plugin
...
}
interface Plugin {
@Composable
fun Content()
}
My use case is, in my app we can dynamically load plugins depending on user requirements. These plugins themselves have composable methods to draw UI, and their own ViewModels in turn.
Thanks for any help!Adam Powell
05/31/2021, 8:05 PM@Composable
function to be like a factory for UI rather than UI itself. When a composable is called during composition it creates an instance of that UI in the composition, not in your ViewModel
or Plugin
. As such, it's as safe to do what your example does as it is to keep a factory in your ViewModel
- if the Plugin
maintains private state based on calls to Content
you could potentially get yourself into trouble, but hopefully it's enough to say, "don't do that" 🙂Chris Fillmore
05/31/2021, 8:06 PMAdam Powell
05/31/2021, 8:08 PMContent
isn't configured to clean up for you; if a Plugin
maintained private state using `DisposableEffect`s you'd be fine, since the effect would clean up itself and presumably that lingering private state when the call to Content
leaves the composition/the composition is torn downChris Fillmore
05/31/2021, 8:11 PM