Is System.exit(0) really that dangerous?

自作多情 提交于 2019-12-02 01:31:51

System.exit(0) is an artifact from Java runtime, it isn't meant for Android. So in any cases using it would be worst solution.

Why don't you use Activity.finish() gracefully?

If you terminate the process you are living in, you'll loose most of the caching and restart time (~resume in the eyes of the user) for it next time will be higher.

Read more in Activity Lifecycle documentation on Android Developers.

Killing the process will not clean up any registered resources from outside the process. BroadcastReceivers, for example. This is a leak and the device will tell you as much.

You really shouldn't be updating the database schema from a background service. Do it when your activities resume.

If you are just updating your data, resuming an activity should validate the data specified by the Intent and tell the user if, for example, Item X is no longer there.

No tool is that dangerous if used carefully and for a specific, well thought off purpose.

However, In your case I do not believe System.exit() is the right way to go. If your application depends on data from a database, create a background service (or a few, depending on what you need) that will inform your application of changes and update the data. It is, in my opinion the right way to handle changes.

As for scenarios when you want to use System.exit() I personally sometimes use it when I can't recover from a critical error and no graceful degradation is possible. In those cases it is better to force all resources associated with your application to cease rather than just leave loose ends tangling around. To clarify, you should always use error handling before doing anything radical. Proper error handling is often the way to go.

But this is a very delicate topic and you are likely to receive quite a few diverging answers.

Therefore my activities are becoming outdated.

Use a ContentProvider and ContentObserver (or the Loader framework), or use a message bus (LocalBroadcastManager, Otto, etc.) to update the activities in situ.

Activity intents also contain outdated params so onCreate, onResume will crash the application

Copy the relevant "params" to data members of the activities. Update those data members as needed (e.g., from the handlers from the message bus-raised events). Hold onto that data as part of your instance state for configuration change (e.g., onSaveInstanceState()). Use this data from onCreate(), onResume(), etc.

An easiest solution is to restart whole application

It is not easiest, if you value your users, as your users will not appreciate your app spontaneously evaporating while they are using it. Do you think that Gmail crashes their own app every time an email comes in?

Next, you will propose writing a Web app that uses some exploit to crash the browser, because you cannot figure out how to update a Web page.

I noticed that ACRA has following code executed after an exception has been handled.

A top-level exception handler is about the only sensible place to have this sort of code, and even there, the objective is for this code to never run (i.e., do not have an unhandled exception).

Brad Seman

There's an existing answer HERE that might give you some help as to why people say it's bad to use System.Exit().

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