title: how to get callback between fragments in kotlin
body: <p>I am trying to get a callback from one child fragment to parent fragment in kotlin and then later call a function in parent fragment to manage other layouts inside the child fragment.</p>
  
  <p>I know how to get callback from a fragment to the activity and access the function in the fragment in the callback we cannot do that in Kotlin because of companion objects.</p>
  
  <p>Another way is to pass context of fragment to child fragment and implement the interface using fragment's content in <code>onAttach()</code> but I cannot pass the context of parent fragment via constructor as I am using factory static methods. </p>
  
  <p>Any help will be highly appreciated.</p>
  
  <p>Child Fragment : <code>DriverStatusFragment.kt</code></p>
  
  <pre><code>  companion object {
      @JvmStatic
      fun newInstance() =
          DriverStatusFragment().apply {
              arguments = Bundle().apply {
              }
          }
  }
  
  override fun onAttach(context: Context) {
      super.onAttach(context)
  
      if (context is OnCurrentOrderStatusChanged) {
          orderStatusChangedCallBack = context
      }
  }
  
  private fun handleUserOnlineStatus(isOnline: Boolean) {
      CustomPreferences.setBooleanPreference(context!!, PrefEntity.IS_ONLINE, isOnline)
  
      when (isOnline) {
          true -&gt; {
              binding.idOnlineStatus.text = getString(R.string.online)
              binding.idOnlineStatusImage.setImageResource(R.drawable.circle_green)
  
              //testing:
              orderStatusChangedCallBack?.orderStatusChanged(CONSTANTS.NEW_ORDER)
          }
          false -&gt; {
              binding.idOnlineStatus.text = getString(R.string.offline)
              binding.idOnlineStatusImage.setImageResource(R.drawable.circle_gray)
          }
      }
  }
  </code></pre>
  
  <p>Parent Fragment : <code>HomeFragment.kt</code></p>
  
  <pre><code>class HomeFragment : Fragment(), OnMapReadyCallback, OnCurrentOrderStatusChanged {
  
  companion object {
      @JvmStatic
      fun newInstance() =
          HomeFragment().apply {
              arguments = Bundle().apply {
              }
          }
  }
  
  fun handleOrderStatus(status: String) {
  
      when (status) {
          CONSTANTS.IDLE -&gt; {
              replaceFragment(
                  DriverStatusFragment.newInstance(),
                  DriverStatusFragment::class.java.simpleName
              )
          }
  
          CONSTANTS.NEW_ORDER -&gt; {
              replaceFragment(
                  NewOrderFragment.newInstance(false),
                  NewOrderFragment::class.java.simpleName
              )
          }
  
          CONSTANTS.ORDER_ACCEPTED -&gt; {
              replaceFragment(
                  EnRouteFragment.newInstance(CONSTANTS.ORDER_ACCEPTED),
                  EnRouteFragment::class.java.simpleName
              )
          }
  
          CONSTANTS.ARRIVED_AT_DROP_LOCATION -&gt; {
              replaceFragment(
                  OrderDeliveredFragment.newInstance(CONSTANTS.ARRIVED_AT_DROP_LOCATION),
                  OrderDeliveredFragment::class.java.simpleName
              )
          }
  
          CONSTANTS.DELIVERED -&gt; {
              replaceFragment(
                  DriverStatusFragment.newInstance(),
                  DriverStatusFragment::class.java.simpleName
              )
          }
      }
  
  
  override fun onAttach(context: Context) {
      super.onAttach(context)
      this.context = context
  }
  
  **NOT RECEIVING CALLBACK HERE**
  
  override fun orderStatusChanged(orderStatus: String) {
      CommonUtils.showToast(context!!, "REAched here")
      handleOrderStatus(orderStatus)
  }
  
  
  
  }
  </code></pre>