-1

If my app encounters a particular error, I want to end my app without any trace of it remaining in the system. I have already seen many threads on this topic, specially these two.

Is quitting an application frowned upon?

How to quit android application programmatically

The answers are great but the proposed solutions are too tedious and/or unnecessarily complex.

Community
  • 1
  • 1
keylime
  • 29
  • 4
  • 1
    It probably would've been better for you to add a comment to one of the above questions than to ask your own question and immediately answer it. – spartygw Jan 07 '15 at 20:38
  • It took me a decent amount of time to find this solution even after going through all the other questions and then doing my own hit and trial. Answering or commenting on other questions would still keep my approach somewhere down there on the page where most users won't see it and hence will not be able to apply it. Hence I created a new question so that others can quickly find the solution. So, I don't think it deserves a minus vote, but it's perfectly ok to have your own opinion. – keylime Jan 07 '15 at 22:51
  • And btw, I tried commenting on other questions, but as a new user, the system wouldn't allow me to comment. And it's preferred to post your question with an answer per http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions which I looked at before posting my question and answering it. – keylime Jan 07 '15 at 22:52
  • 1
    @dvs: Then you should have posted your answer on the original question. There is no need to create a duplicate when two old questions already exist on it. – corsair992 Jan 08 '15 at 04:23

1 Answers1

-1

Taking a cue from Neil Traft's answer in the first post above, I have found a perfect solution for such scenarios.

System.exit(0) or android.os.Process.killProcess(android.os.Process.myPid()) both seem to work if you have only one Activity in the app's backstack, else Android just relaunches your app pushing the (top - 1) Activity to the foreground, maybe because Android assumes that the user was interacting with the app and it suddenly "crashed", so he should continue interacting with it.

So, my solution is to first send the app into background using Activity.moveTaskToBack() and then invoke either of the two methods above.

private void terminateApp() {       

    // TODO: Don't forget to clean up your background threads, services, etc. 

    // send the app into background, otherwise Android will relaunch the app process if you have multiple Activities in your backstack.
    moveTaskToBack(true);

    // kill everything running in this process.
    System.exit(-1); // or you can use android.os.Process.killProcess(android.os.Process.myPid());
}

I don't see any problem with this approach except that it goes against Android design principles... though this works perfectly when you really, really need it.

Hope this helps someone!

keylime
  • 29
  • 4
  • "Taking a cue" is different from plagiarizing and then spam-posting links to your question to get reputation points. This will likely backfire. – 323go Jan 07 '15 at 20:56
  • Where did you see the same solution to say that this is a plagiarization? Let me know and i will delete my question (hopefully there is a way to do it) – keylime Jan 07 '15 at 22:54
  • This is the same answer as Neil Traft's answer, which you are referring to. You took the two essential bits, but left out the explanation, which made his answer good. – 323go Jan 08 '15 at 13:04
  • And yes, @dws, you can delete your question just fine. You already deleted the two spam-posts (or maybe they were deleted for you by our friendly moderators). Once you delete the question, you'll get your reputation points back, which will get you closer to being able to comment. – 323go Jan 08 '15 at 13:05
  • @323go: This is not plagiarism, as there is attribution to the source, which is all that is required by the [Creative Commons Attribution-ShareAlike](http://creativecommons.org/licenses/by-sa/3.0/) license that all Stack Overflow content is licensed under. Not is it an exact duplicate, as it does provide a solution not mentioned in the original answer, even though it derives from the explanation in there. The _question_ on the other hand is a duplicate of that question, and should be closed as such. – corsair992 Jan 08 '15 at 15:47
  • It's fine to disagree. The way I see it, the question was duplicate, and then dvs picked one of the answers, extracted the two critical points from it, and copied it in condensed form. The answer provided is actually considerably *less* helpful than the original answer, as the reasoning for those two two lines was omitted, along with the appropriate warnings against such hacks. Where this becomes plagiarism for me is when he says "taking a cue," rather than "refining" or "copying." And maybe that's just how I understand it, but his phrase implies his own creativity, which is lacking here. – 323go Jan 08 '15 at 19:32
  • @323go: I don't agree that this answer does not add anything over the original. The critical explanation is provided (minus the extended philosophy), and a solution is compiled that _derives_ from the the original, but is not one of the actual solutions/advice provided there. "Taking a cue" is not an incorrect phrasing therefore, "copying" certainly _is_ incorrect, "refining" would also be somewhat correct but with slightly different connotations. The original idea is attributed to the author, but the actual solution is the poster's. – corsair992 Jan 08 '15 at 20:13
  • We'll disagree on this, and that's fine. But you have now twice stated that the question was a duplicate -- why haven't you voted it as duplicate, yet? – 323go Jan 08 '15 at 20:47
  • 1
    @323go: I flagged it as a duplicate. I don't have the privileges for voting to close. – corsair992 Jan 08 '15 at 20:54