Can i use viewmodel factories along with hilt dependency injection?

187 views Asked by At

I am using mvvm architecture and injecting the repository in the viewmodel using hilt. There is a variable in the activity (i am getting the variable in the activity through an intent) which i need to pass to the viewmodel and i thought viewmodel factories might help. But how to use it alongside hilt?

1

There are 1 answers

0
Henrique Vasconcellos On BEST ANSWER

I am not aware of hilt implementing any sort of "assisted injection" for viewmodels, like the assisted injection library from square. But you can easily manage dynamic data within your viewmodel using this sample from this google sample.

    fun setLogin(login: String?) {
        if (_login.value != login) {
            _login.value = login
        }
    }

with this code you will only update the livedata if the value is null, thus even if the fragment rotates, the data will remain the same.

If you wanna check a slight outdated assisted injection project, I have this one in Kotlin.