Hilt injection ViewModel cannot pass parameters
See original GitHub issueMy project has 1 Activity、 1 Fragment 、1 ViewModel
HiltViewModel
class HiltViewModel @ViewModelInject constructor(
) : ViewModel() {
val mName: MutableLiveData<String> = MutableLiveData()
}
HitAppCompatActivity
@AndroidEntryPoint
class HitAppCompatActivity : AppCompatActivity() {
private val mHitViewModule: HiltViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hilt)
mHitViewModule.mName.postValue("google")
}
}
HiltFragment
@AndroidEntryPoint
class HiltFragment : Fragment() {
private val mHitViewModule: HiltViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("HiltFragment", "HiltFragment 1111 ")
mHitViewModule.mName.observe(activity, Observer {
Log.e("HiltFragment", "HiltFragment 222 name = " + it)
})
}
}
I cannot get parameters (name) int the HiltFragment?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
How to pass runtime parameters to a ViewModel's constructor ...
I'm trying to figure out how to pass the itemId that I get from the ItemFragment's NavArgs to the ItemViewModel's constructor? Is there...
Read more >3 Ways to Tackle Assisted Injection for ViewModels
All you need to do is to annotate your ViewModel class with @HiltViewModel and give a SavedStateHandle parameter in the constructor.
Read more >Use Hilt with other Jetpack libraries - Android Developers
Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject annotation in the ViewModel object's constructor. Kotlin Java ...
Read more >Hilt: Inject Runtime parameters to ViewModels. - Carrion.dev
This is necessary to inject parameters into ViewModel constructor and be able to execute some code in the init function.
Read more >Injecting ViewModel with Dagger Hilt | by Elye - Medium
We cannot just use KTX viewModels() extension function since we need to pass these three parameters to ViewModel. We will need to provide...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll close this since there is nothing actionable from our side, if you ever go back to the issue you encountered when passing parameters to a Fragment ViewModel and can provide us with more details then maybe we can take a look. Thanks
If you want to pass arguments to this fragment scoped viewModel let's say repository/Manager, can be created via module with FragmentComponent (hilt annotation). Similarly Fragment arguments can be consumed via SavedStateHandle using @Assisted(hilt annotation).
@raviteja786143 regarding the quoted comment. I tried this approach without much luck. However I did not delve to far into the cause of the error as I found a workaround by avoiding passing parameters to the Fragment’s ViewModel.
I do however believe that there still exists an issue passing parameters to a Fragment ViewModel but I am unable to provide any more details since I was happy with my workaround.