Esinor I.D.
Esinor I.D.

Reputation: 73

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.

Upvotes: 3

Views: 7111

Answers (4)

mattgmg1990
mattgmg1990

Reputation: 5876

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

Upvotes: 5

Rahul Verma
Rahul Verma

Reputation: 2150

this is possible by overriding the back button . do it like this :

public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode==KeyEvent.KEYCODE_BACK)
{
  if (comprobations == true){
          finish();
       } else {
          NOT EXIT!!
       }
 }
}

Upvotes: 0

Simon
Simon

Reputation: 14472

You can't do this, since it is Android that calls them.

Either put your check before you finish() the activity, or trap the back key.

if (!comprobations){
     this.finish():
}

Or:

public boolean onKeyDown(int keyCode, KeyEvent event){

    if(keyCode == KeyEvent.KEYCODE_BACK) {
        if (!comprobations){
            this.finish():
            return true;
        }

        return false;

    }
}

Upvotes: 0

Aleks G
Aleks G

Reputation: 57316

The simple answer is "you cannot do this" or "this is not possible". Your application cannot prevent the user from closing it or the system from killing it when it (the system) runs low on resources.

And, in case you are comparing with some built-in apps (e.g. Samsung's setup), remember that those "special" apps have access to the internals of the operating system and are not operating on the same level as your application.

Upvotes: 4

Related Questions