0

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!

Mr. B.
  • 8,041
  • 14
  • 67
  • 117

1 Answers1

1

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.

Bugsnag Support
  • 381
  • 1
  • 4
  • Thanks, Matt! It worked after moving `Bugsnag.init(this);` under `Thread.setDefaultUncaughtExceptionHandler(...)`. – Mr. B. Jan 30 '19 at 09:10
  • *"Note that killing the process and launching a new intent is not an approach we'd recommend in general in an Android app."* --- I'm restarting the app when it breaks. It's the worst case scenario and I'm trying my best to handle all Exceptions. Do you have an alternative way? Thanks! – Mr. B. Jan 30 '19 at 09:11