How to handle thrown exceptions and then delegate processing back to the system?

I want to send a firebase crash report for every uncaught exception in my application, but I also want the Android system to display the "Application has stopped responding" dialog box. How should I do it? I already have an UncaughtException handler that sends a crash report to firebase. My problem now allows Android to handle the rest of the process.

+4
source share
2 answers

An uncaught exception can be delegated back to the system, preserving the old exception handler and passing fuzzy exceptions to it.

First create a class Applicationas shown below:

public class Controller extends Application {

    private static Thread.UncaughtExceptionHandler defaultHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        if (defaultHandler == null) {
            defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        }
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                FirebaseCrash.report(e); //or whatever
                defaultHandler.uncaughtException(t, e); //this will show crash dialog.
            }
        });
    }

}

:

<application
    android:name=".Controller"
    ... />
+1
0

Source: https://habr.com/ru/post/1683677/


All Articles