javaandroidexceptionerror-handlingbugsnag

Android/Java: UncaughtExceptionHandler and Bugsnag in parallel?


I'm using Bugsnag for automated error reporting and would like to perform some additional actions, like a restart, when the app crashes.

Problem: both is working individually, but not combined. As soon as I add MyUncaughtExceptionHandler, Bugsnag stops reporting.

App

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        Bugsnag.init(this);

        // "Breaks" Bugsnag:
        Thread.setDefaultUncaughtExceptionHandler(
            new MyUncaughtExceptionHandler(this, MainActivity.class));
    }
}

MyUncaughtExceptionHandler

public class MyUncaughtExceptionHandler implements
    java.lang.Thread.UncaughtExceptionHandler {

    private final Context mContext;
    private final Class<?> mActivityClass;

    public MyUncaughtExceptionHandler(Context context, Class<?> c) {
        mContext = context;
        mActivityClass = c;
    }

    public void uncaughtException(Thread thread, Throwable exception) {
        Bugsnag.notify(exception);
        Intent intent = new Intent(mContext, mActivityClass);
        mContext.startActivity(intent); // restarts the app

        Process.killProcess(Process.myPid());
        System.exit(0);
    }
}

I already tried to call Bugsnag.notify(exception) within MyUncaughtExceptionHandler.

Any idea? Thanks in advance!


Solution

  • I spotted your post here but do always reach out to us via Bugsnag support if you want a guaranteed response.

    The issue is that when you're calling Thread.setUncaughtExceptionHandler, you're removing the handler which Bugsnag sets up here: https://github.com/bugsnag/bugsnag-android/blob/2308eb6c706f66495dea116acf619f695530dff4/sdk/src/main/java/com/bugsnag/android/ExceptionHandler.java#L31

    You'd need to call the original handler in order for Bugsnag to detect anything.

    Note that killing the process and launching a new intent is not an approach we'd recommend in general in an Android app.