title: BaseFragment with viewbinding
body: <p>To use <code>viewbinding</code> in an android app I am basically creating base classes for <code>Activity</code> &amp; <code>Fragment</code> to remove boilerplate of everytime writing inflating code.</p>
  <p><strong>ACTIVITY:</strong></p>
  <p><code>BaseActivity</code> with <code>viewbinding</code>:</p>
  <pre><code>abstract class BaseActivity&lt;VB : ViewBinding&gt; : AppCompatActivity() {
      
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          binding = getViewBinding()
      }
  
      abstract fun getViewBinding(): VB
  
  }
  </code></pre>
  <p><code>MainActivity</code>:</p>
  <pre><code>class MainActivity : BaseActivity&lt;ActivityMainBinding&gt;() {
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(binding.root)
          //we can directly use binding now and it works fine inside activity
          //binding.view.doSomething() 
      }
  
   override fun getViewBinding(): ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)
  }
  </code></pre>
  <p><strong>FRAGMENTS</strong> :</p>
  <p><code>BaseFragment</code>:</p>
  <pre><code>abstract class BaseFragment&lt;VB : ViewBinding&gt; : Fragment() {
  
      var binding: VB? = null
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          binding = getViewBinding(view)
      }
  
      abstract fun getViewBinding(view: View): VB
  }
  </code></pre>
  <p><code>DemoFragment</code>:</p>
  <pre><code>class DemoFragment : BaseFragment&lt;DemoFragmentBinding&gt;() {
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          //problem is here
          binding.txtData.text=&quot;Something&quot;
      }
  
      override fun getViewBinding(view: View): DemoFragmentBinding = DemoFragmentBinding.bind(view)
  
  }
  </code></pre>
  <p>demo_fragment.xml:</p>
  <pre><code>&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.ui.fragments.DemoFragment&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/txt_data&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:text=&quot;Hello&quot; /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  <blockquote>
  <p>Problem : Unable to access views using binding inside Demofragment. I don't know why it works with activity and not with fragment.</p>
  </blockquote>
  <blockquote>
  <p>2nd way that I don't want todo:</p>
  </blockquote>
  <p><em>implementation 'androidx.fragment:fragment-ktx:1.3.1'</em></p>
  <pre><code>class DemoFragment : Fragment(R.layout.demo_fragment) {
  
      lateinit var binding: DemoFragmentBinding
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          binding = DemoFragmentBinding.bind(view).apply {
              txtData.text = &quot;Hello World&quot;
          }
      }
  }
  </code></pre>