0

I need to override the physical button event with some logic but also the event itself of going back. The way I'm doing only works if I don't use any logic inside of the callback, I need to perform de logic and be able to going back.

This is how I'm doing.

val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
       if(Hawk.contains("gtw")){
           Hawk.delete("gtw");
       }
    }

I tried using isEnabled but sill doesn't work!

Nathiel Paulino
  • 534
  • 6
  • 17
  • When you add a callback, you are responsible for handling back. Are you saying your callback isn't being triggered? If it is being triggered, then it is up to you to make sure it is actually handling back exactly how you want it to. – ianhanniballake Feb 21 '21 at 07:36
  • It was triggered, but it doesn't goes back to the previous fragment! That's what I want to do, triggers the callback, remove the item and then goes back. – Nathiel Paulino Feb 21 '21 at 07:40
  • But your code doesn't do the "and then goes back" part, so that doesn't happen. Is there a reason you're not calling `parentFragmentManager.popBackStack()` or the equivalent to pop the back stack as part of you handling back? – ianhanniballake Feb 21 '21 at 16:39

1 Answers1

2

As much as I have understood from your question you need to do some logic whenever user presses back button from the app or from your device so for that :-

You need to override activiy's lifecycle method named as onBackPressed(). You can override that and write your required logic inside of that. It will be called whenever the user presses back button from the device itself or if you go back programatically from your activity.

eg :-

override fun onBackPressed() {
        super.onBackPressed()
        // Your logic here 
}

Update as mentioned in comments if you need to go back to your previous fragment from the current fragment then you need to get the callback in your current activity of that fragment. Now in that current activity you'll need to remove that fragment from your stack so you can check if there's any other fragments in the stack then you can simply pop your current fragment or else you can execute the activity's onBackpressed itself.

You can refer this question on S.O and apply your own logic of handling backpressed in your fragments.

WhiteSpidy.
  • 1,107
  • 1
  • 6
  • 28
  • It's inside of a fragment, that's why I'm using like that https://medium.com/@pavan.careers5208/onbackpresseddispatcher-android-2a771f25bd44 – Nathiel Paulino Feb 21 '21 at 07:33
  • if that's in fragment then i guess you'll need to add `activity.onBackpressed` and handle that according to your requirements in your activity – WhiteSpidy. Feb 21 '21 at 07:39
  • According to that article `onBackPressed` is handled by the fragment. In activity, the default behaviour is blocked by this conditon whether the back press is handled on fragment. That means you need to navigate back yourself inside the callback after your logic, right? – Varsha Kulkarni Feb 21 '21 at 08:02