Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where will code stop running when app is killed from task manager on Android

Tags:

java

android

If a user is running my app and decides to kill it via the task manager, are there any guarantees about how much code will be executed before the process dies?

I feel like it could stop running at any arbitrary line, however I guess I don't really know the answer to this.

An example use case is for stopping and restarting a background alarm. If I wanted to cancel an alarm, do some work, then reset the alarm, I feel like there is a chance that the app could stop running after the 'cancel alarm' step but before the 'reset alarm' step. This would leave me in a state that I don't want to be in because I always want my alarm to be set to go off in the future. (Keep in mind this is just an example, I'm not looking for workarounds to this specific case).

Is there anything I can do to ensure that code my java code runs as a 'transaction' similar to a db transaction so that it either all runs or none of it runs?

like image 292
neonDion Avatar asked Nov 17 '15 15:11

neonDion


1 Answers

If a user is running my app and decides to kill it via the task manager, are there any guarantees about how much code will be executed before the process dies?

Not really. If the user elects to use Force Stop in Settings, your process is terminated immediately with extreme prejudice. In other situations where your process is terminated, Android usually will destroy outstanding activities and services, but that is not guaranteed either, particularly if Android is in a rush to free up system RAM (e.g., to handle an incoming phone call). Removing the task from the overview screen is not really distinctive here.

Is there anything I can do to ensure that code my java code runs as a 'transaction' similar to a db transaction so that it either all runs or none of it runs?

No.

like image 95
CommonsWare Avatar answered Oct 07 '22 18:10

CommonsWare