0

I've been trying to implement an custom UncaughtExceptionHandler into my application and I've discovered the library Ereza/CustomActivityOnCrash.

I have added all the code as described in the README (https://github.com/Ereza/CustomActivityOnCrash) but I still can't seem to get it to work. When I force my app to crash, a white screen pops up and then immediately disappears.

Any ideas what might be causing this? below is some of my code

AndroidManifest

    <activity
        android:name="com.test.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.test.MainActivity"
        android:label="MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="cat.ereza.customactivityoncrash.RESTART" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.test.ErrorActivity"
        android:label="Error"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="cat.ereza.customactivityoncrash.ERROR" />
        </intent-filter>
    </activity>

Application class

public class TestApplication extends Application {

    private static TestApplication mInstance;
    private TimeZone usersDefaultTimeZone;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        //Customization for CustomActivityOnCrash
        //Show (TRUE) or Hide (FALSE) the stack trace on the error activity
        CustomActivityOnCrash.setShowErrorDetails(false);

        //Enable Restart (TRUE) 'Restart App' button
        //Disable Restart (FALSE) 'Close App' button
        CustomActivityOnCrash.setEnableAppRestart(true);

        //This sets a custom error activity class instead of the default one.
        CustomActivityOnCrash.setErrorActivityClass(ErrorActivity.class);

        //This sets a EventListener to be notified of events regarding the error activity,
        //CustomActivityOnCrash.setEventListener(new CustomEventListener());

        //Specify the Activity to to restart the application
        CustomActivityOnCrash.setRestartActivityClass(MainActivity.class);

        //Install CustomActivityOnCrash
        CustomActivityOnCrash.install(this);

        //Now initialize your error handlers as normal
        Fabric.with(this, new Crashlytics());
    }

    public static synchronized TestApplication getInstance() {
        return mInstance;
    }

    static class CustomEventListener implements CustomActivityOnCrash.EventListener{

        @Override
        public void onLaunchErrorActivity() {
            Log.i("BookingApp", "onLaunchErrorActivity()");
            Crashlytics.logException(new Exception("Unknown Exception Caught & Error screen displayed to user"));
        }

        @Override
        public void onRestartAppFromErrorActivity() {
            Log.i("BookingApp", "onRestartAppFromErrorActivity()");
            Answers.getInstance().logCustom(new CustomEvent("Restart After Crash"));
        }

        @Override
        public void onCloseAppFromErrorActivity() {
            Log.i("BookingApp", "onCloseAppFromErrorActivity()");
            Answers.getInstance().logCustom(new CustomEvent("Close After Crash"));
        }
    }
}
RheeBee
  • 195
  • 2
  • 11

3 Answers3

2

Hi put following code in Application class and modify as you want for handeling unhandled exceptions:

  @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable e) {
                handleUncaughtException(thread, e);
            }
        });
    }

    public void handleUncaughtException(Thread thread, Throwable e) {

            e.printStackTrace(); // not all Android versions will print the stack trace automatically


//Work what you want to do....

//start new activity with clearing task
 Intent intent = new Intent(this, ActivityClass.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

//end
            System.exit(2); // kill off the crashed app
        }

Hope this help, I am using in my app... works.

Ramesh Kumar
  • 1,229
  • 14
  • 24
0

After some more investigation I discovered that this library/code doesn't like being run in debug mode. If I installed the application on my device and launched it from there (rather than using Debug from Android Studio), it seemed to work fine.

RheeBee
  • 195
  • 2
  • 11
0

This is most likely caused by the ErrorActivity not being specified as part of another process.

From the library documentation, regarding the usage of a custom error activity:

If you use this, the activity must be declared in your AndroidManifest.xml, with process set to :error_activity.

Ereza
  • 1,434
  • 3
  • 15
  • 18