title: Passing arguments to AndroidViewModel
body: <p>I'm following the google <a href="https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#8" rel="nofollow noreferrer">tutorial</a> for Room persistence but i'm stuck, right now I have the tutorial all working fine but I need to expand it and be able to pass parameters to the <code>ViewModel</code> because what I need is to be able to submit different queries to the repo, and maybe i'm wrong but right now i'm doing it in the <code>ViewModel</code> which should be able to read his field and choose the right method to talk with the repo.</p>
  
  <p><strong>WordViewModel:</strong></p>
  
  <pre><code>public class WordViewModel extends AndroidViewModel {
  
   private WordRepository mRepository;
  
     private LiveData&lt;List&lt;Word&gt;&gt; mAllWords;
     public int mode = 0;       
  
     public WordViewModel (Application application) {
         super(application);
         mRepository = new WordRepository(application);
         if (mode==0)
           mAllWords = mRepository.getAllWords();
         else
           mAllWords = mRepository.getSomethingElse();
     }
  
     LiveData&lt;List&lt;Word&gt;&gt; getAllWords() { return mAllWords; }
  
     public void insert(Word word) { mRepository.insert(word); }
  }
  </code></pre>
  
  <p>Then in the activity the triggers the model view we got this</p>
  
  <pre><code>mWordViewModel = ViewModelProviders.of(this).get(WordViewModel.class);
  mWordViewModel.mode=1; //MY ADDITION, not working
  ...
  mWordViewModel.getAllWords().observe(this, new Observer&lt;List&lt;Word&gt;&gt;() {
     @Override
     public void onChanged(@Nullable final List&lt;Word&gt; words) {
         // Update the cached copy of the words in the adapter.
         adapter.setWords(words);
     }
  });
  ...
  </code></pre>
  
  <p>Now the problem is that the field access and edit (the "mode" field) i've made is not working, it's like the field is getting resetted when the <code>ViewModel</code> is actually called and so it's always 0. What am i Missing? What is the easiest workaround considering that mode is just for explaining and eventually i'll need a lot of parameters (so creating various <code>ViewModel</code> is not an option)</p>