Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent the user from closing my app?

I've an android application that will be used in a restaurant, so I want that users can't exit from the app. The only thing that users can, is using application.

(If possible only admin can exit from app, by logging in or restarting device, I don't know which is the best way).

Is there a solution or other way to do this?

like image 809
Nerd Avatar asked Apr 07 '13 11:04

Nerd


People also ask

How do I stop apps from being closed?

Open the Settings and find the “Apps” section. From the app's information page, select “Force Stop” or “Force Close.”

What happens when you close app?

What happens when you close apps on Android? Closing an app stops it from running in the background. It won't sign you out of any apps, but opening the app again will start a new instance. You will need to keep an app running in the background if you want to continue where you left off.

Can you shut down all the applications?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go. Swipe from left to right.


1 Answers

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.

like image 169
Kosh Avatar answered Oct 13 '22 23:10

Kosh