title: Good way to pass a reference to a ViewModel in an activity android
body: <p>I have a <code>ViewModel</code> that inherits from a base class and I would like to have a corresponding <code>Activity</code> also inherit from a base class. The activity would call the same method of the derived <code>ViewModel</code> each time.  So it would be something like this:</p>
  
  <p><strong>BaseViewModel:</strong> </p>
  
  <pre><code>abstract class BaseViewModel(application: Application) : AndroidViewModel(application) {
      protected val context = getApplication&lt;Application&gt;().applicationContext
  
      protected var speechManager: SpeechRecognizerManager? = null
  
      var _actionToTake : MutableLiveData&lt;AnalyseVoiceResults.Actions&gt; = MutableLiveData()
      var actionToTake : LiveData&lt;AnalyseVoiceResults.Actions&gt; = _actionToTake
  
      open fun stopListening() {
          if (speechManager != null) {
          speechManager?.destroy()
          speechManager = null
      }
  
      open fun startListening() {
          val isListening = speechManager?.ismIsListening() ?: false
          if (speechManager == null) {
             SetSpeechListener()
          } else if (!isListening) {
             speechManager?.destroy()
             SetSpeechListener()
          }
  
      }
  }
  </code></pre>
  
  <p><strong>BaseActivity</strong>  </p>
  
  <pre><code>class BaseActivity : AppCompatActivity() {
  
      private lateinit var baseViewModel: BaseViewModel
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      }
  
      fun goback() {
          super.onBackPressed()
          baseViewModel.stopListening()
          finish()
      }
  
      fun startListening() { 
          baseViewModel.startListening()
      }
  
      override fun onDestroy() {
          super.onDestroy()
          baseViewModel.stopListening()
      }
  }
  </code></pre>
  
  <p><strong>Derived Activity:</strong></p>
  
  <pre><code>class DerivedActivity : BaseActivity() {
  
      private val nextActivityViewModel: NextActivityViewModel by inject()
                                      ///^^inherits from BaseViewModel
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
  
  
          /*** pass reference ***/
          baseViewModel = nexActivityViewModel
  
          nextActivityViewModel.actionToTake.observe(this, object : Observer&lt;AnalyseVoiceResults.Actions?&gt; {
              override fun onChanged(t: AnalyseVoiceResults.Actions?) {
                  if (t?.equals(AnalyseVoiceResults.Actions.GO_BACK) ?: false) {
                      goback()
                  }
              }
           })
  
          startListening()
      }
  }
  </code></pre>
  
  <p>Will this cause memory leaks to have two instances of a view model for this activity?  Is there a better way to do this? I don't want to keep repeating the same code for all my activities.  (I would also have the same question if I was doing this with one base fragment).           </p>