Search code examples
androidlinuxoperating-systemsystemcpu-architecture

what happens when user abruptly shuts app in android?


Suppose a user is running an app and then suddenly he closes the app.
Now, will the process in which the app was currently running be immediately killed?
If the answer to the above question is yes then it will mean at the end of some calculation that was being done in my app regarding which I had to save something to the database which could not be done because the process was killed. is it so?


Solution

  • Android guarantees to keep your process while the app has either an active Activity or a foreground Service. Right after your Activity has received onPause() event and there are no foreground Services running, the process hosting your app may be killed at any time, which means that even the onStop() is not guaranteed to be invoked in some cases. Thus, you should save the important data right in the onPause() lifecycle callback or use foreground Services.

    Look here for details: Activity lifecycle, App's process lifecycle