question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hilt injection ViewModel cannot pass parameters

See original GitHub issue

My 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:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
danysantiagocommented, Jul 8, 2020

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

0reactions
goldy1992commented, Jul 5, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found