Search code examples
javaandroidonpauseactivity-finishhome-button

Kill app after 'Home' is pressed (stop it from just pausing)


Whenever the home button is pressed, onPause is called, however I have set onPause to handle pausing a drawing thread when a dialog is opened. This means that when you press the home button, it pauses the drawing thread and keeps the current activity open, however I want to close the activity/app. How can I stop the home button from calling onPause and make it finish the Activity?

EDIT: I have just realised that the app is no longer visible, so onStop() should be called, however it is not. What could be causing this?


Solution

  • Override onUserLeaveHint() and finish/kill your activity/process accordingly.

    E.g.,

    @Override
    public void onUserLeaveHint() {
        finish();
        super.onUserLeaveHint();
    }
    

    UPDATE

    Create a local boolean variable to track the button click that shows the dialog. Set it true as soon as user clicks the button; in all other cases (in onResume(), once started drawing thread, after returning from the dialog, etc) keep it false. In onPause() pause the drawing thread only if that variable is true.