System.exit() in android

无人久伴 提交于 2019-12-18 04:46:20

问题


I know system.exit(0) should not be used. I have read plenty of tutorials as well stating why it's not recommended for exiting applications and finish() is a better alternative ,but in very rare case when this dirty workaround is used than my main question is can it harm the android device or any aspect of device if used?


回答1:


short answer: No.

long answer: No, it doesn't harm the device or any aspect of the device. It just removes the app from memory and cleans up all used resources. If you have any files open, they can become corrupted, but the filesystem won't. Android should release all and any resources (GPS, WiFi, etc) that you have in use at the time, but they could be in an undefined state. Compare the effects of System.exit() to a an app-crash: that wouldn't affect your device either.




回答2:


Calling System.exit(0) anywhere outside the "main" method of an application is not recommended for the following reasons.

  1. It is an impediment to reusing your code.

  2. It makes unit testing hard. For example, if your code calls System.exit when some tests exercise some error handling, it will end the test on encountering System.exit(0).




回答3:


It will not harm your device. The only tricky bit is that Android could bring your app back to life immediately because of the asynchronous intent broadcast/receiving architecture. Let's say you just broacasted something that a activity of your app listens to. And then you exit. But android will bring your app back to life to handle the broadcast.



来源:https://stackoverflow.com/questions/11257449/system-exit-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!