0

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.

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.

Another way is to pass context of fragment to child fragment and implement the interface using fragment's content in onAttach() but I cannot pass the context of parent fragment via constructor as I am using factory static methods.

Any help will be highly appreciated.

Child Fragment : DriverStatusFragment.kt

  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 -> {
            binding.idOnlineStatus.text = getString(R.string.online)
            binding.idOnlineStatusImage.setImageResource(R.drawable.circle_green)

            //testing:
            orderStatusChangedCallBack?.orderStatusChanged(CONSTANTS.NEW_ORDER)
        }
        false -> {
            binding.idOnlineStatus.text = getString(R.string.offline)
            binding.idOnlineStatusImage.setImageResource(R.drawable.circle_gray)
        }
    }
}

Parent Fragment : HomeFragment.kt

class HomeFragment : Fragment(), OnMapReadyCallback, OnCurrentOrderStatusChanged {

companion object {
    @JvmStatic
    fun newInstance() =
        HomeFragment().apply {
            arguments = Bundle().apply {
            }
        }
}

fun handleOrderStatus(status: String) {

    when (status) {
        CONSTANTS.IDLE -> {
            replaceFragment(
                DriverStatusFragment.newInstance(),
                DriverStatusFragment::class.java.simpleName
            )
        }

        CONSTANTS.NEW_ORDER -> {
            replaceFragment(
                NewOrderFragment.newInstance(false),
                NewOrderFragment::class.java.simpleName
            )
        }

        CONSTANTS.ORDER_ACCEPTED -> {
            replaceFragment(
                EnRouteFragment.newInstance(CONSTANTS.ORDER_ACCEPTED),
                EnRouteFragment::class.java.simpleName
            )
        }

        CONSTANTS.ARRIVED_AT_DROP_LOCATION -> {
            replaceFragment(
                OrderDeliveredFragment.newInstance(CONSTANTS.ARRIVED_AT_DROP_LOCATION),
                OrderDeliveredFragment::class.java.simpleName
            )
        }

        CONSTANTS.DELIVERED -> {
            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)
}



}
Lovish-Pandey
  • 135
  • 1
  • 15
  • 1
    An excellent example can be found here https://stackoverflow.com/questions/39491655/communication-between-nested-fragments-in-android – sadat Mar 04 '20 at 10:45
  • 1
    just use [`getParentFragment()`](https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getparentfragment)? – Pawel Mar 04 '20 at 11:29
  • @sadat thanks for the direction!. It worked! Thank you very much. – Lovish-Pandey Mar 05 '20 at 07:50

0 Answers0