title: Android: preferred way to create a listener, function vs inner class vs object
body: <p>Currently, I am creating a simple <code>CompoundButton.OnCheckedChangeListener</code> inside my fragment. My problem is, that I have three ways to create it:</p>
  <h2>Solution 1</h2>
  <pre><code>fun switchListener() : CompoundButton.OnCheckedChangeListener {
      return CompoundButton.OnCheckedChangeListener { buttonView, isChecked -&gt;
          if (isChecked) rebuildViewModel
      }
  }
  </code></pre>
  <h2>Solution 2</h2>
  <pre><code>inner class SwitchClass : CompoundButton.OnCheckedChangeListener {
      override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
          if (isChecked) rebuildViewModel
      }
  }
  </code></pre>
  <h2>Solution 3</h2>
  <pre><code>object SwitchListener : CompoundButton.OnCheckedChangeListener {
      override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
          if(isChecked) rebuildViewModel // unresolved reference
      }
  }
  </code></pre>
  <p>My question is, what are the major differences between those implementations? The only difference I see is that <code>rebuildViewModel</code> is not accessible inside my <code>object SwitchListener</code>. Why is that so? What implementation should I prefer?</p>