Regarding the background state:
By default, there is no need to kill the application process manually. Android OS does this on its own if there is a need to free resources for other applications.
See this guide for reference.
Although if you need to do some background work during this "downtime", you can start the Service to perform these operations and then stop it from the code.
Regarding the foreground state:
I think that the best approach to use here is to send messages to the Handler of the main thread of your application, because you do not know if the user will interact with the user interface after he leaves. When the user returns to the user interface, you can clear the message queue using the Handler removeMessages method.
I do not recommend terminating the process using System.exit (0) on Android.
source share