title: Android MVVM - How to reference Activity in ViewModel
body: <p>MVVM architecture,</p>
  
  <p>this is my View (Activity):</p>
  
  <pre><code>private MyApp app;
  private MainActivityVM viewModel;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  app = (MyApp) this.getApplication();
  ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
  
  MainActivityVM.Factory factory = new MainActivityVM.Factory(app);
  
  final MainActivityVM model = ViewModelProviders.of(this, factory)
  .get(MainActivityVM.class);
  
  viewModel = model;
  binding.setVm(viewModel);
  viewModel.onCreate();
  </code></pre>
  
  <p>and View Model:</p>
  
  <pre><code>public class MainActivityVM extends AndroidViewModel implements ViewModel {
  
      public MainActivityVM(@NonNull Application application) {
          super(application);
      }
  
      @Override public void onCreate() {
           model = new MyService();
           model.getData(); /* &lt;-- how do i pass the activity here? */
      }
  
      @Override public void onPause() { }
  
      @Override public void onResume() { }
  
      @Override public void onDestroy() { }
  
      public static class Factory extends ViewModelProvider.NewInstanceFactory {
  
          @NonNull
          private final Application mApplication;
  
          public Factory(@NonNull Application application) {
              mApplication = application;
          }
  
          @Override
          public &lt;T extends android.arch.lifecycle.ViewModel&gt; T create(Class&lt;T&gt; modelClass) {
              return (T) new MainActivityVM(mApplication);
          }
      }
  }
  </code></pre>
  
  <p>and Model:</p>
  
  <pre><code>public class myService{
  
      public getData(){
          if(permissionacquired(){
              getdata()
          }else{
              requestPermission();
          }
      }
  
      private void requestPermission() {
          PermissionKey permKey = new PermissionKey(HealthConstants.StepCount.HEALTH_DATA_TYPE, PermissionType.READ);
          HealthPermissionManager pmsManager = new HealthPermissionManager(mStore);
          try {
              // Show user permission UI for allowing user to change options
  
              /* BELOW CODE REQUIRE Activity reference to PASS */
  
              pmsManager.requestPermissions(Collections.singleton(permKey), MainActivity.this).setResultListener(result -&gt; {
  
              /* ABOVE CODE REQUIRE Activity reference to PASS */
  
              Log.d(APP_TAG, "Permission callback is received.");
              Map&lt;PermissionKey, Boolean&gt; resultMap = result.getResultMap();
  
                  if (resultMap.containsValue(Boolean.FALSE)) {
                      updateStepCountView("");
                      showPermissionAlarmDialog();
                  } else {
                      // Get the current step count and display it
                      mReporter.start(mStepCountObserver);
                  }
              });
          } catch (Exception e) { Log.e(APP_TAG, "Permission setting fails.", e); }
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> if you see my request permission in my Model, the API require activity to be pass - how can i pass activity reference to the request permission?</p>
  
  <p>I have a get permission method that comes from Model. this get permission method from my service provider require activity e.g. <code>requestPermission(Activity)</code></p>
  
  <p>so in my ModelView, i have the model object which is the dataService from another source.</p>
  
  <p>then, how I can reference Activity in my ViewModel so I can call: <code>model.requestPermission(Activity);</code> in my ViewModel?</p>
  
  <p>understanding from <a href="https://developer.android.com/topic/libraries/architecture/viewmodel.html" rel="noreferrer">here</a> that:</p>
  
  <blockquote>
    <p><strong>Caution</strong>: A ViewModel must never reference a view, Lifecycle, or any
    class that may hold a reference to the activity context.</p>
  </blockquote>