Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPause not called when back button pressed?

It was my understanding, obviously wrong, that onPause() is called whenever the back button is pressed? Now in my code I've put this onPause() event:

@Override
protected void onPause(){
    super.onPause();        

    if(!_END_GAME){
        Builder _alert = new AlertDialog.Builder(this)
        .setMessage("onPause, with game NOT over!");
        _alert.setNeutralButton("OK.",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss(); // Kills the interface
                System.runFinalizersOnExit(true);
                finish();
            }
        });
        _alert.setTitle("Your Score!");
        _alert.show();
    }

}

Now the problem is, the dialog does not launch what-so-ever, and then the code errors out. I put the dialog there to try to visualize where the onPause() was called and help me debug some other variables and such. Yet like I said it never even gets shown. Any ideas why this would be? Is there a function that is launched prior to onPause() when the back button is pressed? Thank you in advance for any info.

like image 838
While-E Avatar asked Apr 06 '11 21:04

While-E


People also ask

What happens to activity when back button is pressed?

Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.

What happens when we press back button in Android?

Depending on the user's Android device, this button might be a physical button or a software button. Android maintains a back stack of destinations as the user navigates throughout your application. This usually allows Android to properly navigate to previous destinations when the Back button is pressed.

Is onPause always called?

Even if your activity slips into the background when another activity starts or when the screen switches off, onPause() is always called even if the other two methods aren't called. So even if activity ceases, onPause() will be called and your thread will be killed.

Which method is not called when we press the Home button from an activity?

Note: onDestroy() method not call after press Home Button.


1 Answers

onPause will always be called when your activity is no longer in the foreground, that's guaranteed. Maybe your _END_GAME is not false? Add a debug log output to your onPause method, you'll see that it always gets called.

I should note though that displaying a dialog during onPause is extremely bad form - the user is trying to get rid of your app (could even be because of an incoming phone call). You DO NOT want a dialog then. That goes against the Android design.

In fact, the Android OS will simply short-circuit your app if you try to do lengthy shenanigans in onDestroy or onPause. Basically, if those get called, you're supposed to disappear quietly.

If you really want to intercept the back button, you can check for the button like Ted suggested, but keep in mind that your app can go to the background in many other ways - home button, selected notification, incoming phone call, etc.

like image 112
EboMike Avatar answered Oct 12 '22 08:10

EboMike