<@UHAJKUSTU> Could you please advise if I can manu...
# decompose
e
@Arkadii Ivanov Could you please advise if I can manually trigger component’s back handling from the fragment’s method? I have single-activity legacy code with fragments and it’s own back-handling. One of the fragments has decompose component inside with Compose UI. I want to be able to pass back events from fragment to that component and receive a Boolean meaning if the navigation was changed inside the component or not (it is already in the top level). The method of the fragment looks like
Copy code
override fun consumedBackPressed(): Boolean
If it returns true the activity will do nothing and if it returns false the activity will change fragments according to it’s own logic.
a
Usually this works automatically, please see the docs. The root component gets attached to the
OnBackPressedDispatcher
and handles back presses automatically. If you really want to use something like
consumeBackPress
, then you can also try creating
OnBackPressedDispatcher
manually and then use it. But keep in mind that this variant is not compatible with the predictive back gesture. The proper way is as described in the docs.
Copy code
import android.os.Bundle
import androidx.activity.OnBackPressedDispatcher
import androidx.fragment.app.Fragment
import com.arkivanov.decompose.defaultComponentContext
import com.arkivanov.sample.shared.root.DefaultRootComponent
import com.arkivanov.sample.shared.root.RootComponent

class MyFragment : Fragment() {
    private val onBackPressedDispatcher = OnBackPressedDispatcher()
    private lateinit var rootComponent: RootComponent

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        rootComponent = DefaultRootComponent(
            componentContext = defaultComponentContext(
                onBackPressedDispatcher = onBackPressedDispatcher,
            )
        )
    }

    fun consumedBackPressed(): Boolean {
        if (onBackPressedDispatcher.hasEnabledCallbacks()) {
            onBackPressedDispatcher.onBackPressed()
            return true
        }

        return false
    }
}
e
Is it possible to get back the information from the component if it was able to consume the back event?
a
It will 100% consume if there is at least one enabled callback registered, i.e. hasEnabledCallback() returned true.
e
What I want to achieve is to trigger main (legacy) navigation when the component doesn’t have any screens in backstack. It is not possible?
a
That's how I described it. If there is a screen to pop, then there will be a registered and enabled back callback.
But as I mentioned, this model is incompatible with the predictive back gesture.
e
Ah, I probably didn’t understand your point. So does the Decompose component itself manage the hasEnabledCallback flag?
a
Yes, it does! It registers callbacks when there is a screen to pop, and unregisters when there are none.
e
That’s great, will try it now! Thank you so much!
👍 1
It works! Thank you again! 💪decompose intensifies🔥
a
Glad to help!
👍 1
d
Happy to learn this too
🙌 1