Home > other >  when click back button, the app crashs after a seceond
when click back button, the app crashs after a seceond

Time:04-05

I have customized back button. when I click on it, the application crashes after a second. here is the code: enter image description here

it looks for active callbacks and if found any calls them and returns. that's what makes the loop.

your code should be:

isEnabled = false
requireActivity().onBackPressedDispatcher.onBackPressed()

CodePudding user response:

replace

requireActivity().onBackPressedDispatcher.onBackPressed()

with just

finish()

currently your onBackPressedDispatcher set in Fragment is calling onBackPressed in (required Activity), which will at first try to propagate this event to own childrens - Fragments. so, again, onBackPressedDispatcher will be triggered, again it will call Activitys method and this is loop

edit: isEnabled = false is a better approach, just wanted to leave you some case description

  • Related