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.

Pass parameter to viewmodel

See original GitHub issue

Hi, It is actually dagger 2.10 specific question. But I think no problem to ask here. My question is, what if you want to pass RepoView to your RepoViewModel? In my case I am using ViewModel. I am not using LiveData to observe. So I need to pass RepoView to RepoViewModel as parameter.

@Inject
public RepoViewModel(RepoRepository repository, RepoView repoView)
@Module
public class RepoModule {

    @Provides
    RepoView provideRepoView(RepoFragment repoFragment){
        return repoFragment;
    }
}
@ContributesAndroidInjector(modules = RepoModule.class)
    abstract RepoFragment contributeRepoFragment();

I implemented it in this way but it gives me error compile time. It says that I didn’t provide RepoView to inject RepoViewModel. RepoView can not bi provided without @Provides-annotation method. Am I missing something? @JoseAlcerreca @yigit

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

10reactions
JoseAlcerrecacommented, Jun 2, 2017

Don’t pass your view (fragment) to your ViewModel, that’s rule number one of ViewModels 😃 This avoids leaks.

See https://developer.android.com/topic/libraries/architecture/viewmodel.html

Note: Since the ViewModel outlives specific activity and fragment instantiations, it should never reference a View, or any class that may hold a reference to the activity context. If the ViewModel needs the Application context (for example, to find a system service), it can extend the AndroidViewModel class and have a constructor that receives the Application in the constructor (since Application class extends Context).

8reactions
yigitcommented, Jun 3, 2017

You should not pass a callback to your ViewModel. If the View is retained, the callback (assuming it references back to the views) will be obsolete.

Instead, your ViewModel can provide observable action events on which your UI can react.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android ViewModel additional arguments - Stack Overflow
So I don't know how to pass additional argument String param into my custom ViewModel . I can only pass Application context, but...
Read more >
Advanced ViewModels (part I): Dependencies and Passing ...
This post focuses on which are the dependencies we will need and how to pass parameters to our ViewModel. Other posts in this...
Read more >
How to add additional parameters to ViewModel via Kotlin
If you want to make it takes parameters, you need to make a new FactoryClass for each view model. But with Kotlin, it...
Read more >
Parameter Injection for Android ViewModels
We can see in the following setup, Compose Navigation only allows us to pass parameters as part of the navigation route String.
Read more >
Xamarin Passing parameters directly to ViewModel
would like to pass an Assignment_Id which is of type Guid to AssignmentViewModel. How do I do that? If you want to transfer...
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