Home > Net >  Previous Back Stack entry value allways null and live data never emits
Previous Back Stack entry value allways null and live data never emits

Time:02-10

So I have a bit of a complex flow. For navigation, we're using Jetpack navigation component.

So I have a Fragment A that lives in module A. When a button is hit it likes to Fragment B in Module B.

From there Fragment B will open up a webview that will deep link to a bottom sheet fragment C in module B.

I need to send the result from Fragment C back to Fragment A.

Fragment C will pass a value to Fragment B

Fragment A emission:

findNavController().previousBackStackEntry?.savedStateHandle?.set<Boolean>(AppsAndDevicesFragment.CONNECTED_CLOUD_DEVICE, true)

Fragment B watches:

override fun onAttach(context: Context) {
    super.onAttach(context)
    findNavControllerSafely()?.currentBackStackEntry?.savedStateHandle?.getLiveData<Boolean>(
        CONNECTED_CLOUD_DEVICE
    )?.observeForever { result ->
        findNavController().previousBackStackEntry?.savedStateHandle?.set(
            WEARABLE_CONNECTION_OBSERVABLE, result)
    }
}

Fragment B does properly trigger the observer.

However in Fragment A, it never fires, and every time I try and fetch the value, it's null

Fragment A listener:

override fun onAttach(context: Context) {
    super.onAttach(context)
    val hasConnected = findNavControllerSafely()?.currentBackStackEntry?.savedStateHandle?.get<Boolean>(AppsAndDevicesFragment.WEARABLE_CONNECTION_OBSERVABLE)
    if (hasConnected == true) {
        viewModel.addProgramToJourney(CampaignMode.AUTOMATIC)
    }
    findNavControllerSafely()?.currentBackStackEntry?.savedStateHandle?.getLiveData<Boolean>(
        AppsAndDevicesFragment.WEARABLE_CONNECTION_OBSERVABLE
    )?.observeForever { result ->
        if (result) {
            viewModel.addProgramToJourney(CampaignMode.AUTOMATIC)
        }
        findNavControllerSafely()?.currentBackStackEntry?.savedStateHandle?.remove<Boolean>(
            AppsAndDevicesFragment.WEARABLE_CONNECTION_OBSERVABLE
        )
    }
}

I don't understand why Fragment A never gets the value.

CodePudding user response:

  •  Tags:  
  • Related