Hello All, I am facing a problem, I had an app wh...
# android
q
Hello All, I am facing a problem, I had an app which is hybird XML and Compose, all bottom navigation screens are XML expect one its Compose and it have all its screen as Composables the problem is when I am using BackHandler on one of thoses Composables it seems like the app is only listen for the XML back dispatcher not my Composables any ideas how to solve this ?
r
You mean the app is only listening for the back handler in your fragment/activity?
q
yes
its like its ignoring the Compose BackHandler
r
If you are overriding
onBackPressed
in your activity, you should call
super.onBackPressed
at the end.
q
I guess this is a deprecated function
r
doesn't stop you from using it
I am only making assumptions; Assuming you have a
BackPressedDispatcher
in one of your fragments, you'll have to either disable the dispatcher or make a further call to the activity's
onBackPressed
to propagate the event to other observers in the chain. This could be your fragment's
onAttach
method;
Copy code
override fun onAttach(context: Context) {
        super.onAttach(context)

        requireActivity().onBackPressedDispatcher
            .addCallback(
                owner = this,
                enabled = true // set this to false
            ) {
                Log.d("Test", "onBackPressed: handled by fragment!")

                requireActivity().onBackPressed() // or call the activity's onBackPressed
            }
    }