Search code examples
androidandroid-activityonpause

How to cancel the onPause or the onStop method?


EDITED & SOVLED (below)

I need to make a kind of last-check when the user is leaving the Activity.
If the result of this check is unsuccess, I need to not allow him to go out.

I need a kind of this:

@Override
protected void onPause() {
    if (comprobations == true){
        super.onPause();
        } else {
            NOT EXIT!!
        }
}

It seems easy :p but I tried and don't runs.

Anyone can help me?

Thaanks

SOLUTION:

Thanks to all of you for the quick answers!
Every of them helped me and I have learned some new things.

I have tried the onBackPressed() method, this is what I was looking for. I know that the user is still able to exit from the App by pressing the Home button and some another ways, but I just want to prevent users to think the configuration of my App is ok when they go back to the main menu; if they wish to go out from the App, it's ok.
So I will do something as this:

@Override
public void onBackPressed() {
    Log.d(LOGTAG, "onBackPressed");
    if (COMPROBATIONS() == true){
        super.onBackPressed();
    } else {
        SHOW WARNING!
    }
    return;
}


Thanks to everybody fo your help.
Have a nice time.


Solution

  • There is nothing you can do to prevent the user from leaving your Activity. Once the onPause method has been called, the application is going into the background and there is nothing you can do to stop it.

    If you really want to do something when the user is leaving your activity, it's possible to override the back button by overriding the on onBackPressed() method and get it to do some other action. However, the user will still be able to leave in a variety of ways (pressing the home button for example).