1

I have seen that people do not recommend using System.exit(0) in Android (not java) to close an application over various forums including here at SO here

-- there's another component running in your process that the system is aware of. Calling exit() in this case would terminate the process, killing your other component and potentially corrupting your data. The OS could care less of course, but your users might not appreciate it. :-)

and here.

But at no place it is explained in detail that what could be repercussions of using it.

I am looking for a detailed explanation may be with an official reference (unfortunately I was not able to find one)

Community
  • 1
  • 1
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • 1
    The first answer answers it all in my opinion. – Patrick Hofman Oct 03 '15 at 16:28
  • try reading this post as well http://stackoverflow.com/questions/16480867/system-exit0-doesnt-close-all-my-activities. – jsh Oct 03 '15 at 16:29
  • 4
    The "official reference" comes from comments from Android engineers, such as Dianne Hackborn: "If the application has left its activity stack in a bad state and does this in the background, the stack is still being maintained by the system and will be restored as per its last state when the application's process is re-created. Also system.exit() can break the behavior of an app, so it should be avoided." (from [this Google Groups post](https://groups.google.com/d/msg/android-developers/Zhd1fRfHAAQ/Xwvnku2hbrEJ)). – CommonsWare Oct 03 '15 at 16:33
  • 2
    Also: "To be clear: using System.exit() is strongly recommended against, and can cause some poor interactions with the system. Please don't design your app to need it" (from [this Google Groups post](https://groups.google.com/d/msg/android-developers/Y96KnN_6RqM/K8EdddZjawoJ)). – CommonsWare Oct 03 '15 at 16:33
  • @CommonsWare yes, It is strongly recommend against. I think I have went through those references. I am looking for some enlightenment on why is it? – Rahul Tiwari Oct 03 '15 at 16:45
  • You're certainly welcome to rummage through the Android source code and determine the precise behavior. – CommonsWare Oct 03 '15 at 16:46
  • @PatrickHofman I am looking for repercussions in details apart from one I quoted in updated question. – Rahul Tiwari Oct 03 '15 at 19:35
  • @CommonsWare actually I found myself giving advice against it. but I knew that my opinion is based on what I read on internet. This is an undocumented advice. – Rahul Tiwari Oct 03 '15 at 20:47
  • 1
    Not everything that we as developers need to worry about in Android is formally documented. Many items are picked up from discussions with Google engineers, in person or online (Google Groups, G+, here on Stack Overflow, etc.). As far as I am concerned, Dianne Hackborn's statements carry the weight of documentation, until and unless countervailing statements are made from some other Googler. You may find that to be insufficient, and that's your call, but you will find Android to be very frustrating if that is the case. – CommonsWare Oct 03 '15 at 20:52
  • @CommonsWare Well I got your point after looking at the signature in this [entertaining discussion](https://groups.google.com/d/msg/android-developers/Zhd1fRfHAAQ/Xwvnku2hbrEJ) . – Rahul Tiwari Oct 03 '15 at 21:05

1 Answers1

3

System.exit(0) does not kill your app if you have more than one activity on the stack. What actually happens is that the process is killed and immediately restarted with one fewer activity on the stack.

for details you can refer this

Is quitting an application frowned upon?

Community
  • 1
  • 1
Awadesh
  • 3,530
  • 2
  • 20
  • 32
  • hii @RahulTiwari please elaborate it. what i found from the referred answer is this. Derive all activities from a common AtivityBase. Then hold a flag to force close the app, in onResume check if the force close flag is set. If so call System.exit(0); This will cascade through the entire activity stack and finally get the app completely closed. – Awadesh Oct 03 '15 at 16:49
  • sorry I meant "what are the repercussions" ? – Rahul Tiwari Oct 03 '15 at 19:36