I am looking for a way to exit an Android application by code. Yes, I know, I shouldn't have done this, and android does this when you click the back button, but I have a stream configured that forces me to implement this. I’ve searched for a while and found several answers:
- This should not be done => No option.
- The finish () call in my activity => Only for 1 activity, and not for the entire application.
- Run the intent for the first action with a special flag => I have several entry points, so it’s not really an option.
- Just kill my own process => Not sure if it will call the onDestroy () method in all actions? And will my background services work with this? (Which should not be stopped)
So, I am wondering if there is another way to exit my application, or am I really limited by these options? If there really is no other option, I decided to create a list of all instances of my actions in my application class, and then just loop them around to call them finish (). But I don't know if this will be the right way to handle this.
So why am I asking my question here, what is the best way to close my application?
Edit: I have more or less fixed the problem. I have a list of WeakReference of my activities. In each onCreate, I add activity to the list. Then, when I want to exit my application, I just iterate over the list and call finish (). Problem: if the list becomes too large, it will not complete all my actions, since the android has already destroyed them. Therefore, every time I know for sure that I no longer need them, I finish them. In my case, the list cannot exceed 3/4 of the action, so you no longer have to worry about the actions not being completed. Also with this method I do not need to kill my own process.
source
share