title: Applying MVVM between ViewModel and Fragment/Activity interactions and communication
body: <p>I am investing a lot of time into properly understanding of architecture components and the how everything fits into MVVM pattern. So far here's how I see things (without doing Dagger injection as I haven't got that far yet):</p>
  
  <p>a) <code>UserEntity</code> is a class with <code>@Entity</code> annotation to handle <code>Room</code>'s table creation</p>
  
  <pre><code>@Entity(tableName="users")
  public class Users{
     private long id;
     private String name;
  }
  </code></pre>
  
  <p>b) <code>User</code> pojo class in the model to use it around the app and has supplemental fields as needed.</p>
  
  <pre><code>public class User{
     private long id;
     private String name;
     private List&lt;Role&gt; roles;
     private Preferences preferences;
  }
  </code></pre>
  
  <p>beside this there can be different pojos depending on what I need from db, for instance, <code>UserWithRoles</code></p>
  
  <p>c) <code>UserDao</code> takes care of getting or inserting/updating info in the room database. In here, for <code>@Insert</code>, <code>@Update</code> I can user the <code>UserEntity</code> but for <code>@Query</code> I can also use the pojo classes</p>
  
  <pre><code>@Dao 
  public abstract class UserDao{
      @Insert
      public abstract long insertUser(User user)
  
     @Query("Select someFields from inner joined tables")
     public abstract LiveData&lt;List&lt;UserRoles&gt; getUsersWithRoles();
  }
  </code></pre>
  
  <p>d) Have <code>RepositoryUser</code> as the repository between ViewModel and Dao</p>
  
  <pre><code>public class RepositoryUser{
         private UserDao userDao;
  
        public RepositoryUser(Application app){
              Database db = Databaase.getDatabase(app.getApplicationContext);
              userDao = db.userDao(); 
        }
        public LiveData&lt;List&lt;UserWithRoles&gt;&gt; getUsersWithRoles(){
              return userDao.getUsersWithRoles()
        }
      }
  </code></pre>
  
  <p>e) <code>UserWithRolesViewModel</code> to be available for the fragment that shows the list with users and their roles</p>
  
  <pre><code>public class UserWithRolesViewModel extends AndroidViewModel{
    private RepositoryUser repositoryUser;
  
    public UserWithRolesViewModel(Application app){
           super(app);
           repositoryUser = new RepositoryUser(app);
    }
  
    public LiveData&lt;List&lt;UserWithRoles&gt;&gt; getUsersWithRoles(){
                  return repositoryUser.getUsersWithRoles()
            }
  }
  </code></pre>
  
  <p>f) In my fragment I can do something like:</p>
  
  <pre><code>public void onCreate(...){
      viewModel = ViewModelProviders.of(this).get(UserWithRolesViewModel.class);
  }
  
  public View onCreateView(...){
    viewModel.getUsersWithRoles().observe(...)
            public void onChanged(...){
            adapter.setData(...);
         }
  }
  </code></pre>
  
  <p>However, there are some pieces that are missing. From my understanding according to MVVM the view should only be responsible for showing info, so no actual logic or even handling to be made inside the fragment or activity. At this point I have 2 questions:</p>
  
  <ol>
  <li>On the regular way, I would create an interface, for instance <code>onFragmentAction</code> and implement it in <code>activity</code>. Then on <code>fragment</code> when I wanted to inform the activity to do something, I would do <code>callback.onFragmentAction(params)</code> and the <code>onFragmentAction</code> in the activity would fire and act accordingly. How is this scenario handled in MVVM? How does a fragment talk to it's parent activity?</li>
  <li>On the regular way I would have inside the fragment's <code>onCreateView</code>, inflate the layout, use <code>findViewById</code> to get the views and use, for instance textView.setText() or button.setOnClickListener(). How can this be done in MVVM? Use DataBinding?  </li>
  </ol>