appyx icon indicating copy to clipboard operation
appyx copied to clipboard

Implicit Navigation and state restoration.

Open ganfra opened this issue 3 years ago • 4 comments

I've a question, given the exemple here: https://bumble-tech.github.io/appyx/navigation/implicit-navigation/#use-case-2

We listen to the UserRepository in the RootNode to then branch the right LoggedInNode or NotLoggedInNode. We make the listening lifecycle aware so we stop listening to UserRepository when RootNode is destroyed.

init {
        userRepository.isLoggedIn()
            .distinctUntilChanged()
            .onEach { isLoggedIn ->
                if (isLoggedIn) {
                        backstack.replace(NavTarget.LoggedInNode)
                    } else {
                        backstack.replace(NavTarget.NotLoggedInNode)
                    }
                }
            .launchIn(lifecycleScope)
    }

So, if we provoke a configuration change, the activity and all the nodes will be destroyed and recreated. The right node will be restored, but we'll also start listening to the UserRepository again, so we'll replace the restored Node by a new one.

Not sure how we can manage this case, any idea?

ganfra avatar Dec 21 '22 18:12 ganfra

Hi @ganfra to fix this quickly without changing presentation model to survive config changes like VM you can try to:

  1. Use newRoot instead of replace if it's logically correct. It will not replace existing navTarget if the new one is equal.
  2. If you can't use newRoot, you can manually check currently active element before replacing it

KovalevAndrey avatar Dec 21 '22 19:12 KovalevAndrey

Thanks for the intels!

I'd avoid using VM as I'd like to get all my dependencies injected into my nodes through dagger... How do you manage config change in your apps?

ganfra avatar Dec 21 '22 19:12 ganfra

We don't use VM either. Our business logic classes implement savedInstanseState plugin to save their states.

KovalevAndrey avatar Dec 21 '22 19:12 KovalevAndrey

@KovalevAndrey : Last question then, how do you handle things like: launching coroutines scoped to the node but which survive config change? (without VM I mean)

ganfra avatar Dec 22 '22 09:12 ganfra