Normally, if the Android app encounters an unhandled exception, a dialog box similar to the following will appear, and then force the app to exit:
If you want to change this default behavior, such as displaying a custom dialog box when an unhandled exception occurs or restarting the application, you can use the following steps to redefine the android global exception handling event.
1. Implement the thread. uncaughtexceptionhandler Interface
Generally, you can derive the application class and implement the thread. uncaughtexceptionhandler method:
Public class gnavigatorapplication extends application implements thread. uncaughtexceptionhandler {...}
2. Define the thread. uncaughtexceptionhandler Method
For example, restarting an activity
Public void uncaughtexception (thread, throwable ex) {intent = new intent (this, gnavigatoractivity. class); intent. addflags (intent. flag_activity_clear_top | intent. flag_activity_new_task); startactivity (intent );}
3. Reset the default exception processing function. For example, you can reset the Exception Processing Method in the oncreate method.
@ Override public void oncreate () {... thread. setdefaultuncaughtexceptionhandler (this );}
In this way, the application will be automatically restarted when an unhandled exception occurs in the application, without the force close dialog box.