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
Rafs
08/06/2023, 9:16 AM
You mean the app is only listening for the back handler in your fragment/activity?
q
Qamar A. Safadi
08/06/2023, 9:17 AM
yes
Qamar A. Safadi
08/06/2023, 9:17 AM
its like its ignoring the Compose BackHandler
r
Rafs
08/06/2023, 9:21 AM
If you are overriding
onBackPressed
in your activity, you should call
super.onBackPressed
at the end.
q
Qamar A. Safadi
08/06/2023, 9:22 AM
I guess this is a deprecated function
r
Rafs
08/06/2023, 9:23 AM
doesn't stop you from using it
Rafs
08/06/2023, 9:43 AM
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
}
}