1

I am trying to implement PIP mode and to do so I am overriding onUserLeaveHint() to register events when the user is leaving the activity, but it is not called when the Back button is pressed, it only registers the home or recents button.

    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
        if (usePipMode){
            setUpPipMode();
        }
    }

So, My Question is, Is it possible to register the back button press in onUserLeaveHint(), and if so please provide some code sample. It would be much helpful.

Thanks.

satya-p91
  • 478
  • 3
  • 12

1 Answers1

3

You can do it with onBackPressed method

@Override
public void onBackPressed() {
   onUserLeaveHint()
}
Usama Altaf
  • 90
  • 1
  • 4
  • 23
  • Thanks for your answer, I was doing this too but I also added the super.onBackPressed(); before. After removing super.onBackPressed(); it is working now. – satya-p91 Jan 15 '21 at 08:41
  • 2
    calling `super.onBackPressed()` will call `finish()` on the `Activity`, which is the normal default behaviour of the BACK button. Calling `finish()` on the `Activity` terminates the `Activity` and goes to the previous `Activity`. – David Wasser Jan 15 '21 at 17:45
  • Yes. Thanks for the detailed explanation. – satya-p91 Jan 18 '21 at 05:36