title: How we can create BaseActivity and BaseFragment in Kotlin?
body: <p>I am new to programming. Can anyone help me, to create the BaseActivity and BaseFragment in Kotlin with use the same in child classes. I do Google but can't clear the topic in my mind. I am sharing source code in below.</p>
  <p><strong>activity_main.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout 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;.MainActivity&quot;
      android:orientation=&quot;vertical&quot;
      android:id=&quot;@+id/activity_Container&quot;&gt;
  
      &lt;include
          layout=&quot;@layout/toolbar_layout&quot;
          android:id=&quot;@+id/toolbar&quot;/&gt;
  
      &lt;FrameLayout
          android:id=&quot;@+id/fragment_Container&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:padding=&quot;0dp&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p><strong>toolbar_layout.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.appcompat.widget.Toolbar xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/toolbar&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:background=&quot;@android:color/white&quot;
      android:elevation=&quot;5dp&quot; /&gt;
  </code></pre>
  <p><strong>BaseActivity.kt</strong></p>
  <pre><code>abstract class BaseActivity : AppCompatActivity() {
  
      private var lastFragmentName:String = &quot;&quot;
      var toolbar: Toolbar? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(getLayoutResource())
          configureToolbar()
      }
  
      protected abstract fun backStackChangeListener(backStackCount: Int)
  
      protected abstract fun getLayoutResource(): Int
  
      open fun configureToolbar(){
          toolbar = findViewById(R.id.toolbar)
          if (toolbar != null){
              setSupportActionBar(toolbar)
              supportActionBar?.setDisplayHomeAsUpEnabled(true)
          }
      }
  
      override fun onSupportNavigateUp(): Boolean {
          onBackPressed()
          return true
      }
  
      fun addFragment(fragment: Fragment) {
          if (fragment != null) {
              val fragmentManager = supportFragmentManager
              fragmentManager.addOnBackStackChangedListener(getListener())
  
              val fragmentTransaction = fragmentManager.beginTransaction()
  
              fragmentTransaction.addToBackStack(fragment.javaClass.simpleName)
              supportFragmentManager.backStackEntryCount
              fragmentTransaction.add(
                  R.id.fragment_Container,
                  fragment,
                  fragment.javaClass.simpleName
              )
              fragmentTransaction.commitAllowingStateLoss()
          }
      }
  
      fun replaceFragment(fragment: Fragment) {
          if (fragment != null) {
              val fragmentManager = supportFragmentManager
              fragmentManager.addOnBackStackChangedListener(getListener())
  
              val fragmentTransaction = fragmentManager.beginTransaction()
  
              fragmentTransaction.addToBackStack(fragment.javaClass.simpleName)
              supportFragmentManager.backStackEntryCount
              fragmentTransaction.replace(
                  R.id.fragment_Container,
                  fragment,
                  fragment.javaClass.simpleName
              )
              fragmentTransaction.commitAllowingStateLoss()
          }
      }
  
      fun removeFragment(fragment: Fragment) {
          if (fragment != null) {
              val fragmentManager = supportFragmentManager
              val fragmentTransaction = fragmentManager.beginTransaction()
  
              fragmentTransaction.remove(fragment)
              fragmentTransaction.commit()
              fragmentManager.popBackStack()
          }
      }
  
      fun removeTopFragment(fragment: Fragment) {
          val fragmentManager = supportFragmentManager
          fragmentManager.popBackStack()
      }
  
      private fun getListener(): FragmentManager.OnBackStackChangedListener {
          return FragmentManager.OnBackStackChangedListener {
              val fragmentManager = supportFragmentManager
              if (fragmentManager != null) {
                  backStackChangeListener(getBackStackCount())
                  val currentFragment = fragmentManager.findFragmentById(R.id.fragment_Container) as BaseFragment
                  if (currentFragment != null &amp;&amp; !lastFragmentName.equals(currentFragment.javaClass.simpleName)) {
                      lastFragmentName = currentFragment.javaClass.simpleName
                      currentFragment.onResume()
                  }
              }
          }
      }
  
      private fun getBackStackCount(): Int {
          val fragmentManager = supportFragmentManager
          return fragmentManager.backStackEntryCount
      }
  }
  </code></pre>
  <p><strong>BaseFragment.kt</strong></p>
  <pre><code>abstract class BaseFragment : Fragment() {
  
      abstract fun getTitle(): Int
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      }
  
      private fun replaceFragment(fragment: Fragment){
          (activity as BaseActivity).replaceFragment(fragment)
      }
  
      private fun addFragment(fragment: Fragment){
          (activity as BaseFragment).addFragment(fragment)
      }
  
      private fun removeTopFragment(){
          (activity as BaseFragment).removeTopFragment()
      }
  
      private fun setTitle(resId: Int){
          activity?.setTitle(resId)
      }
  
      private fun setTitle(title:String){
          activity?.setTitle(title)
      }
  }
  </code></pre>
  <p>Thanks in advance.</p>