'Disable app from closing Android

I need to create an app that cannot be closed on Android, neither by the home button or the back button. Currently I am looking into creating my own ROM as the app does not need to be published it is an internal app for my company, but was thinking if there is any other easier options.



Solution 1:[1]

As mentioned by @Commonsware, you need to make your application a launcher (Homescreen).

Also, You can keep a service running which every half milliseconds check which activity is on top, if it is any other activity, other than yours, then it pushes your activity to top.

You can use the PackageManager to get the current top activity.

Also, the back button press can be easily handled, in your app, just remove the super call in onBackPressed

Solution 2:[2]

Make your app be the home screen.

Solution 3:[3]

Here is one way I found to prevant the back button from closing, you might be able to do the same for the home button.

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

I got this code from a similar question found here Prevent Back button from closing my application

Solution 4:[4]

Be careful when you try this.
To disable back button just override the onBackPressed method:

@Override
public void onBackPressed() {
   // super.onBackPressed();
}

To disable the home button try this

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("HOMEBUTTON", "Home Button PRESSED");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 CommonsWare
Solution 3 Community
Solution 4 Tyler2P