-3

About Android (6.0 to the last version)

I'm developing an app and we want that the user, once he accepts all the terms, don't be able to kill the process or force stop the app. Honestly, I'm completely lost right now, because on the last versions of android, and specially some brands like Xiaomi, we are having a lot of trouble with it, and we don't know how to act right now.

In the case that it could not be possible, could at least get an alert whenever the user is killing the app?

Thanks!!

doubtone
  • 13
  • 1
  • Hey there & welcome to StackOverflow! Your question is incomplete and has little chances to get answered as is. Please read the **[How-To-Ask](https://stackoverflow.com/help/how-to-ask)** article & edit your question adding more information. Let's start with some snippets that you've worked on regarding the matter! (if you need help with this, read up on how to create a **[Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve)**) – iamdanchiv Dec 18 '18 at 13:12
  • 2
    If your program runs on the end user device, he'll always be able to kill it, stop, uninstall or whatever. Being able to do so one of the basics of OS administration, and it's not programs responsability to prevent the user from doing so. – Alejandro Dec 18 '18 at 13:34
  • May I ask why the user shouldn't be allowed to stop your app? Are you trying to solve another problem, like data consistency? As the user, I'd uninstall and rate 1 star for any app that does something against my will. – Robert Dec 18 '18 at 13:58

2 Answers2

1

It is not possible to prevent the user from killing an app. Android is a unique system where the app has no direct control over its lifecycle but the system has. The system can (and will, when required) kill the app or any of its processes at its own will. To make your app aware of these changes, the android framework provides for various callbacks such as onPause, onStop and onDestroy which are called in succession when the user kills the app.

Side Note : There is no guarantee that onDestroy() will be completely executed when the app is killed. Do not place essential code there.

Of course, you can block or try to prevent the user from closing your app by overriding the back, home and recent buttons but it is highly recommended not to do so. Even if you do so successfully, the user has other means to close your app such as rebooting their phone.

So what to do?

You are looking for a kiosk mode app. Kiosk mode is used for single purpose phones such as at a restaurant or for a cab driver. Kiosk mode apps lock down the user to only a specific app (or a specific set of apps).

For normal apps, it is not possible to prevent the user from force closing your app. You can only get alerts by checking for lifecycle changes as described above. Moreover, it is not at all recommended to change the natural behavior of the hardware buttons on android. The user can still find a way to close your app. If your app is doing something really essential which should proceed in the background, consider using a service for that instead. Also, the user can uninstall your app at anytime if they find your app being too intrusive and you won't be able to do anything in that scenario.

Tl;dr: Use kiosk mode to prevent the user from exiting the app. This will only allow the user to access your app(s) in their device.

Yashovardhan99
  • 887
  • 11
  • 26
1

Usually you cannot! Even if you try to disable some buttons, user can always stop app or restart device. In addition at times, the OS will stop the App. Your responsibility as a programmer is to program around this, and give the user the feel that it never stopped. If you are doing background monitoring, you will need to use service. Users will still be able to stop service. Having said that, you can set your app as a Device Administration app, see here, which may disallow stopping, but unless you are distributing internally to a company, noone will install.

lionscribe
  • 3,413
  • 1
  • 16
  • 21