4

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.

Arne517
  • 825
  • 2
  • 8
  • 14
  • Perhaps the `System` class will help (or an Android equivalent)? (I'm not familiar with android programming) – 11684 Mar 02 '12 at 11:21
  • 2
    Please read [this](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – Ghost Mar 02 '12 at 11:26
  • 1
    "I have a customized flow that forces me to implement this" -- this is a bug in your app. Please fix the bug in your app. – CommonsWare Mar 02 '12 at 13:26
  • 1
    Well, it's not really a bug. While navigating through the app, there'll be a moment that when the user presses the back button, he'll expect the app to close (Not every time though). But when I call finish(), the "old" activities will show. And those have to be closed too. So maybe you'll say, that I should finish those activities when I start a new one. But when I start a new Activity, I don't know yet if the user will be able to return to the current activity or not. I know, it sounds complicated :) but it will really make sense that the application closes. – Arne517 Mar 02 '12 at 13:44
  • "there'll be a moment that when the user presses the back button, he'll expect the app to close" -- that is a bug in your design. Please fix your design. – CommonsWare Mar 02 '12 at 14:28
  • 1
    Well, I can "fix the desing bug" by changing the setup of my flow. But this would require me to inflate every activity inside my main activity, and handle history myself. I just think that isn't the right way to do this. Just think about a tabbed application: When you open a tab, it starts a new activity but when you press back, it closes. Also a tabbed activity can open an other activity, where when you press back, it doesn't close. Besides this, in a non-tabbed activity, there'll be a way to "show the tabs". This will cause the user to think he's back at the beginning, while he isn't. – Arne517 Mar 02 '12 at 14:54

3 Answers3

3

You should not do this, but anyways if you insist:

System.exit(0);
Caner
  • 57,267
  • 35
  • 174
  • 180
  • That's also a way I found, forgot the mention it. But the problem with this one is that when I open my application again, it will recreate the activities that were open when I closed my app. – Arne517 Mar 02 '12 at 11:38
3

use below code.

android.os.Process.killProcess(android.os.Process.myPid());
    System.runFinalizersOnExit(true);
SathishBabu S
  • 382
  • 3
  • 11
  • 1
    I had a really nasty need to do this (early versions of Android with OpenGL resource leakage bugs, so I had to kill off the app between invocations to force Android to clean up). I did it by using `android:process="foo"`to force this one activity into its own process, to avoid killing other activities when the process died, and used the above code. It actually worked. – David Given Mar 02 '12 at 11:38
  • And this will call onDestroy on all the opened activities? Also won't this stop my running services? – Arne517 Mar 02 '12 at 11:38
  • All activities and services instantiated in the process will just vanish, exactly as if Android had nuked the process due to low memory. This is why you need to use `android:process`, so that anything you _don't_ want killed lives in a different process. – David Given Mar 02 '12 at 11:40
-1

You can keep a list of all started activities in List (use WeakReference to avoid memory leak). To exit the app, first invoke finish method on each items and then invoke android.os.Process.killProcess(android.os.Process.myPid());

Qiang Jin
  • 4,427
  • 19
  • 16
  • I just tried this, but when I have a lot of open activities (that much that android destroys them, and recreates them when I revisit them), this method doesn't work. It will still recreate those activities. – Arne517 Mar 02 '12 at 13:44
  • @Arne517 Thats's why you should finish all activites first and then invoke killProcess. If you finish them first, they won't be recreated – Qiang Jin Mar 02 '12 at 14:12
  • @CommonsWare I'm not sure why it's not suitable invoke killProcess, sometimes you have to, i mean the app is designed to exit in certain activity, then you should make it happen. I tried other methods but only this works for me – Qiang Jin Mar 02 '12 at 14:16
  • 1
    @solilo: "I'm not sure why it's not suitable invoke killProcess" -- because the core Android team told you not to. "sometimes you have to" -- no, you do not. "i mean the app is designed to exit in certain activity, then you should make it happen" -- that is a flawed design. Next, you'll say that a Web app should cause browsers to crash because some designer said there had to be a way to "exit" the Web app. The user is perfectly capable of pressing the HOME button, bring up recent tasks, or otherwise navigate away from your app. – CommonsWare Mar 02 '12 at 14:27
  • 1
    @CommonsWare Personally I don't like use the HOME button to 'exit' the app. I prefer to 'close' it like what i usually do in windows/linux. User doesn't know the app is 'crash' or just being brought to background, if they cann't tell the difference, i think it' ok to do this.. And about core Android team, i wish them can help me deal with the PMs.. – Qiang Jin Mar 02 '12 at 14:43
  • @solilo: It is most certainly not "ok" to use `killProcess()`. You can always call `moveTaskToBack()` if you want to yield the foreground. – CommonsWare Mar 02 '12 at 14:47
  • 1
    @CommonsWare I tried moveTaskToBack, however, if the app icon is clicked again, the old activity is brought to foregound, but what i need is to open new splash activity just like it's opened first time – Qiang Jin Mar 02 '12 at 14:58