I'm looking for a way to quit my android app by code. Yes, I know, I shouldn't be doing this cause android handles it when you press the back button, but I have a customized flow that forces me to implement this. I've already been searching for a while, and found multiple answers:
- It shouldn't be done => No option.
- Call finish() in my activity => Only for 1 activity, not the whole app.
- Start an Intent to the first activity with a special flag => I have multiple entrypoints, so not really an option.
- Just kill my own process => Not sure about this one, will this call the onDestroy() method in all activities? And will my background services keep running with this? (Which shouldn't be stopped)
So I'm wondering, is there any other option to quit my app, or am I really limited to these options? If there really isn't an other option, then I was thinking to create a list of all the instances of my activities in my Application class, and then just loop them to call finish() on them. But I don't know if this will be the correct way to handle this.
So that's why I ask my question here, which is the best way to close my app?
Edit: I more or less fixed the problem. I have a list of WeakReference of my Activities. In every onCreate I add the activity to the list. Then, when I want to quit my app, I just loop the list and call finish(). Problem being: If the list gets too big, it won't finish all my activities, since android already destroyed them. So every time I know for sure I don't need them anymore, I finish them. In my case, the list can't grow bigger than 3/4 activities, so no need anymore to worry about activities not getting finished. Also with this method, I don't have to destroy my own process.