Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes if call goBack() after navigateTo() #29

Open
nvhaiwork opened this issue Dec 2, 2023 · 1 comment
Open

Crashes if call goBack() after navigateTo() #29

nvhaiwork opened this issue Dec 2, 2023 · 1 comment

Comments

@nvhaiwork
Copy link

Case:
Fragment1 navigateTo() Fragment2
Fragment2 check condition, if data is invalid -> backTo() Fragment1 => crashes

It works if I use Handle.postDelay()

Fragment 1 Code: onViewCreated()

     getView()?.findViewById<TextView>(R.id.text)?.setOnClickListener {
        getView()?.findViewById<TextView>(R.id.text)?.text = "Clicked"
        val options = FragmentNavigator.NavOptions(
            transaction = FragmentNavigator.TransactionType.REPLACE,
            animation = AnimationDefinition.Zoom
        )
        (activity as MainActivity).navigate.navigateTo(Fragment2(),options)
    }

Fragment 2 code: onViewCreated()

     getView()?.findViewById<TextView>(R.id.text)?.setOnClickListener {
        getView()?.findViewById<TextView>(R.id.text)?.text = "Clicked"
        val options = FragmentNavigator.NavOptions(
            transaction = FragmentNavigator.TransactionType.ADD,
            animation = AnimationDefinition.Zoom
        )
        (activity as MainActivity).navigate.navigateTo(Fragment3(),options)
    }

    //Handler().postDelayed({
        (activity as MainActivity).navigate.goBack()
    //}, 1)
@nvhaiwork nvhaiwork changed the title Crash if call goBack() after navigateTo() Crashes if call goBack() after navigateTo() Dec 2, 2023
@KaustubhPatange
Copy link
Owner

KaustubhPatange commented Dec 2, 2023

Fragment transactions are async i.e they are executed using a message queue system. APIs like commit, popBackStack adds a new event to be executed later whenever FragmentManager is free.

There are so-called sync APIs like commitNow, popBackStackImmediate that are not queued but are executed immediately upon called. goBack uses popBackStackImmediate API (I don't remember why I used it but maybe my tests would know why). Whenever any other transaction is in progress and these sync APIs are called they crash with a message "Already executing transactions... something like this".

To fix it, use the async FragmentManager.popBackStack API directly. Navigator does not expose a sync version of goBack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants