Question

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?

Was it helpful?

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.

OTHER TIPS

You can actually override onPause() and call finish() from there.

However, just don't do that. Users expect a common behavior, if you override the way users expect to interact with your app they will be confused.

It actually crashes? Then you should post the LogCat showing the crash.

Perhaps you didn't call super.onPause() in your overridden onPause() method? That's mandatory and if you forget it, the App crashes -- explicitely telling you in the exception that you haven't called super.onPause().

   @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
    }


 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {     

        if(keyCode == KeyEvent.KEYCODE_HOME)
        {
           android.os.Process.killProcess(android.os.Process.myPid());
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top