navigation.pop() does not work in navigation in decompose library

63 views Asked by At

I created an application in jetpack compose using decompose. navigation.pop() does not work when clicking the back arrow in the top left corner. The application freezes and after a while I get this:

ANR in com.example.weatherappcompose (com.example.weatherappcompose/.presentation.MainActivity) Reason: Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: 0. Wait queue length: 1.) Load: 0.0 / 0.0 / 0.0 CPU usage from 144856ms to 0ms ago (2024-02-06 09:06:01.959 to 2024-02-06 09:08:26.816) with 99% awake:

Here is my code:

class DefaultRootComponent @AssistedInject constructor(
    private val detailsComponentFactory: DefaultDetailsComponent.Factory,
    private val favouriteComponentFactory: DefaultFavouriteComponent.Factory,
    private val searchComponentFactory: DefaultSearchComponent.Factory,
    @Assisted("componentContext") componentContext: ComponentContext
) : RootComponent, ComponentContext by componentContext {

    private val navigation = StackNavigation<Config>()
    override val stack: Value<ChildStack<*, RootComponent.Child>> = childStack(
        source = navigation,
        initialConfiguration = Config.Favourite,
        handleBackButton = true,
        childFactory = ::child
    )

    private fun child(
        config: Config, componentContext: ComponentContext
    ): RootComponent.Child {
        return when (config) {
            is Config.Details -> {
                val component = detailsComponentFactory.create(
                    city = config.city, onBackClick = {
                        navigation.pop()
                    }, componentContext = componentContext
                )
                RootComponent.Child.Details(component)
            }

            Config.Favourite -> {
                val component = favouriteComponentFactory.create(onCityItemClicked = {
                    navigation.push(Config.Details(it))
                }, onAddFavouriteClick = {
                    navigation.push(Config.Search(openReason = OpenReason.AddToFavourite))
                }, onSearchClick = {
                    navigation.push(Config.Search(openReason = OpenReason.RegularSearch))
                }, componentContext = componentContext
                )
                RootComponent.Child.Favourite(component)
            }

            is Config.Search -> {
                val component = searchComponentFactory.create(componentContext = componentContext,
                    onClickBack = {
                        navigation.pop()
                    },
                    onClickOpenForecast = {
                        navigation.push(Config.Details(it))
                    },
                    onClickSavedToFavourite = {
                        navigation.pop()
                    },
                    openReason = config.openReason
                )
                RootComponent.Child.Search(component)
            }
        }
    }

    sealed interface Config : Parcelable {

        @Parcelize
        data object Favourite : Config

        @Parcelize
        data class Search(val openReason: OpenReason) : Config


        @Parcelize
        data class Details(val city: City) : Config

    }

    @AssistedFactory
    interface Factory {
        fun create(
            @Assisted("componentContext") componentContext: ComponentContext
        ): DefaultRootComponent
    }
}

I tried downgrading Decompose because I read that this could be a problem too, but it didn't help.

0

There are 0 answers