I've got an interesting case. I don't know how to inject one of my classes (using dagger/hilt) into my composable. I'm using AAC VM + hilt + navigation etc, BUT the wrench in the whole thing is that outside of my root NavHost, I have a "global dialog" for showing things like a force update prompt, etc.
So now I basically have this really simple scenario of
Box {
NavHost
GlobalDialog
}
but my GlobalDialog needs access to a class that I typically inject into my screens. Thoughts?
f
Francesc
06/26/2023, 3:21 PM
You could have a ViewModel at the activity level that you pass to your dialog, and inject into the viewmodel whatever you need in the dialog, then expose the data to the dialog via a state class.
Alternatively you could create a state holder that contains whatever you need to inject, which is injected at the activity and then used to instantiate the state holder, which is passed to the dialog.
s
Stylianos Gakis
06/26/2023, 6:12 PM
Just inject it at your activity directly? And pass it into the composable that needs it.
c
Colton Idle
06/27/2023, 3:12 AM
Injecting into my activity directly. Was curious if maybe I was missing something basic... but yeah the basic activity inject worked as expected. Thanks all for saving my sanity
d
dewildte
06/27/2023, 8:20 PM
Are you using the
NavGraphBuilder.dialog()
function?
dewildte
06/27/2023, 8:21 PM
It sets up a dialog as a navigation destination in the graph.