Skip to content Skip to sidebar Skip to footer

Android Uncaughtexceptionhandler To Finish App

I want to close the app after logging an unhandled exception. After searching here i made the following: public class MyApplication extends Application { //uncaught exceptions

Solution 1:

At last i resolved this making a class implementing the Thread.UncaughtExceptionHandler interface:

publicclassMyUncaughtExceptionHandlerimplementsThread.UncaughtExceptionHandler {

    privateBaseActivity activity;
    privateThread.UncaughtExceptionHandler defaultUEH;

    publicMyUncaughtExceptionHandler(BaseActivity activity) {
        this.activity = activity;
        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }

    publicvoidsetActivity(BaseActivity activity) {
        this.activity = activity;
    }

    @OverridepublicvoiduncaughtException(Thread thread, Throwable ex) {

        //LOGGING CODE//........

        defaultUEH.uncaughtException(thread, ex);

    }
}

In the BaseActivity i added the following code:

//exception handlingprivatestaticMyUncaughtExceptionHandler _unCaughtExceptionHandler;

@OverrideprotectedvoidonCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    if(_unCaughtExceptionHandler == null)
        _unCaughtExceptionHandler = newMyUncaughtExceptionHandler(this);
    else
        _unCaughtExceptionHandler.setActivity(this);

    if(Thread.getDefaultUncaughtExceptionHandler() != _unCaughtExceptionHandler)
        Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

I know its the same code i have in the question, but somehow its working now. When i have more free time i will look this deeply to find the root cause and post it

Post a Comment for "Android Uncaughtexceptionhandler To Finish App"