0

How can I kill an app completely from code?

I know it's possible, because when you click the exit button from Angry Birds, and then check the android task killer thing, it's gone.. but I can't get my own app to do that =/ It still hangs around and preserve's it's last state..which I don't want

I have tried this with no success:

@Override
public void onBackPressed()
{
    // do something on back.
    this.finish();
    return;
}

Thanks for any help on this

CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
  • 4
    Please see [this](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238) post for a thorough and thoughtful response on why force closing is, in general, not something you want to be doing. Is there a reason you want to do this? – Matt Briançon May 01 '11 at 02:32
  • Why would I not? Why should I not be allowed to tell my app when it's time to terminate? ^_- @the response: Ranting. Not one valid fact. – CuriousGeorge May 01 '11 at 04:14

3 Answers3

3

To do what you want, simply call System.exit(0). Is it highly recommended you do NOT do this though.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • That's the solution I didn't want to give, since it is not recommended, at least you mentioned that :) – MByD May 01 '11 at 04:44
  • 3
    Note that calling System.exit(0) will not work as expected (a new process will be spawned) if there are any additional running Services or Activities. In this case you should first finish the other components. –  May 01 '11 at 07:02
  • 1
    @MByD It's not recommended, but hiding the information wouldn't help. If Nick wants to write an app that behaves badly, that's his choice. – Romain Guy May 01 '11 at 17:53
  • I didn't think about it like that, but you are right. @Nick - Sorry. – MByD May 02 '11 at 03:04
0

FYI the finish code does not delete the object itself. It just gets run when cleanup of the object is required just before deletion. Logically Deletion => Call finish() and therefore the opposite is not required to be true (and in this case is not).

I suspect that what you are looking for should actually be specified in the android manifest and it is probably the android:noHist option.

Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
0

You cannot kill your app completely in a legitimate way, there are some hacks for that, but basically no. Android will decide when to kill your app.

I think this link will help you do the most and get you what you want, but this will not kill your app: http://smartandroidians.blogspot.com/2010/01/exiting-android-application.html

MByD
  • 135,866
  • 28
  • 264
  • 277
  • so then how is it that angry birds terminates the way it does? is that just because it's big enough in memory for the OS to decided to delete it? because you can hit the home button on the phone with no effect, but the exit button causes the application to terminate and be destroyed completely.. – CuriousGeorge May 01 '11 at 03:09
  • I think the link I shared will help you understand that. I implemented an app that actually killed itself, but it was a hack for a very specific use, and not for distribution. If you just want to get to main activity next time, look at the link I shared. – MByD May 01 '11 at 03:12
  • @Romain Guy: Thanks! works. I don't understand why I was not able to find this anywhere =/ it doesn't cause any memory leaks does it? @MByD: Thanks, but my app only has one activity that just forwards all input, ect.., to my NDK lib. – CuriousGeorge May 01 '11 at 03:56