How do I prevent the user from closing my app? How do I prevent the user from closing my app? android android

How do I prevent the user from closing my app?


you can override the onBackPressed method

@Override public void onBackPressed(){    Toast.MakeText(getApplicationContext(),"You Are Not Allowed to Exit the App", Toast.LENGTH_SHORT).show();}

this will prevent the back button from exiting the application.

and then you will need to override the home button as well like

public boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_HOME) {        Log.i("TEST", "Home Button");  // here you'll have to do something to prevent the button to go to the home screen         return true;    }    return super.onKeyDown(keyCode, event);}

EDIT: for new devices with android version 4.0.xx you'll have to override the recent apps button as well hope that helps you.