0

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?

TomRichardson
  • 5,933
  • 5
  • 27
  • 30
  • Do you mean that your App stays in front, even when you press the home button? Or is it just that it continues to run in the backend? That would be the normal behaviour. – Ridcully Jun 26 '13 at 10:05
  • @Ridcully - It does not stay at the front, but onPause is called and onStop is never called. I have overridden onPause to handle when a Dialog is opened, and onResume to handle when the Dialog is closed, however when I navigate away with the Home button it never resumes properly and crashes. – TomRichardson Jun 26 '13 at 10:08
  • What kind of dialog is that? Who is showing it? – ozbek Jun 26 '13 at 10:46
  • Speech recognition dialog. The user opens it. – TomRichardson Jun 26 '13 at 10:52
  • Without seeing the code, it will be very difficult for anyone speak to why your code is behaving the way it does. – CommonsWare Jun 26 '13 at 11:05
  • There's no concept of "opening" or "closing" an app in Android. You should speak in terms of the activity life cycle. – Jeffrey Blattman Sep 15 '16 at 19:46

4 Answers4

1

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.

ozbek
  • 20,955
  • 5
  • 61
  • 84
0

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.

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • As I mentioned, I have overridden onPause to handle the Activity being paused when a Dialog is opened, and onResume is used to start redrawing once again after the dialog is closed. I cannot call finish here as the activity would end when the Dialog is opened. – TomRichardson Jun 26 '13 at 10:10
0

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().

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • I have called super.onPause(), and also, sorry, it doesn't crash, it hangs until it is force closed by the "Not responding" message. – TomRichardson Jun 26 '13 at 10:32
-1
   @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());
        }
    });
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37