3

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.

Esinor I.D.
  • 73
  • 2
  • 5
  • I get the slight feeling this is not possible, since this would allow easy kiosk mode (which is not entirely possible in unrooted Android), plus I think Android as OS handles these situations (think of stopping your application by the OS due to low memory, etc). – stealthjong Dec 20 '12 at 15:50

4 Answers4

5

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

mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • Thanks, this was that I was looking for, just to manage some actions for prevent the usre before going back. I was thinking on the lifecyle, but not on the "events"; I need to read a bit more about. Thanks. – Esinor I.D. Dec 21 '12 at 09:10
  • No problem! Yeah, the events are very helpful in getting your app to behave just the way you want it to. The lifecycle methods are more of a way to deal with conditions that are happening no matter what. – mattgmg1990 Dec 21 '12 at 15:01
4

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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

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;

    }
}
Simon
  • 14,407
  • 8
  • 46
  • 61
0

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!!
       }
 }
}
Rahul Verma
  • 2,140
  • 3
  • 21
  • 31