Custom Activity On Crash

repository·master·Indexed 25 days ago

https://github.com/ereza/customactivityoncrash

An Android library that replaces the standard 'Unfortunately, X has stopped' crash dialog with a custom activity. It provides CaocConfig.Builder for behavior customization, support for Firebase Crashlytics and ACRA integration, and utilities for implementing completely custom error activities and crash data collection.

Tokens
1.9K
Snippets
2
Records
8
Agent score
37%

What's inside customactivityoncrash

  1. Customize the default error activity resources

    master

    You can customize the appearance of the default error activity by overriding resources in your app:

    Theme The activity uses your app theme by default (must be a child of Theme.AppCompat). To use a specific theme, declare it in AndroidManifest.xml:

    <activity
        android:name="cat.ereza.customactivityoncrash.activity.DefaultErrorActivity"
        android:theme="@style/YourThemeHere"
        android:process=":error_activity" />

    Image Use errorDrawable(int) in the builder, or provide a drawable named customactivityoncrash_error_image in all density buckets (mdpi, hdpi, etc.).

    Strings Override these string names in your strings.xml:

    • customactivityoncrash_error_activity_error_occurred_explanation
    • customactivityoncrash_error_activity_restart_app
    • customactivityoncrash_error_activity_close_app
    • customactivityoncrash_error_activity_error_details
    • customactivityoncrash_error_activity_error_details_title
    • customactivityoncrash_error_activity_error_details_close
    • customactivityoncrash_error_activity_error_details_copy
    • customactivityoncrash_error_activity_error_details_copied
    • customactivityoncrash_error_activity_error_details_clipboard_label
  2. Incompatibilities and Limitations

    master

    Be aware of the following constraints when using CustomActivityOnCrash:

    • Custom Handlers: It will not work if you set a custom UncaughtExceptionHandler after initializing the library that does not call back to the original handler.
    • Crash Loops: If your app initialization or the error activity itself crashes, an infinite restart loop may occur.
    • Multidex/Multiprocess: The library has not been explicitly tested with multidex enabled or multiprocess applications.
    • Native Errors: It does not catch native (C/C++) errors.
    • ANRs: It will not prevent or catch Application Not Responding (ANR) errors.
  3. Configure Custom Activity On Crash behavior

    master

    Use CaocConfig.Builder to customize the library's behavior. It is recommended to perform this configuration within the onCreate method of your Application class to ensure settings are applied as early as possible.

    @Override
    public void onCreate() {
        super.onCreate();
    
        CaocConfig.Builder.create()
            .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
            .enabled(false) //default: true
            .showErrorDetails(false) //default: true
            .showRestartButton(false) //default: true
            .logErrorOnRestart(false) //default: true
            .trackActivities(true) //default: false
            .minTimeBetweenCrashesMs(2000) //default: 3000
            .errorDrawable(R.drawable.ic_custom_drawable) //default: bug image
            .restartActivity(YourCustomActivity.class) //default: null (your app's launch activity)
            .errorActivity(YourCustomErrorActivity.class) //default: null (default error activity)
            .eventListener(new YourCustomEventListener()) //default: null
            .customCrashDataCollector(new YourCustomCrashDataCollector()) //default: null
            .apply();
    }
  4. Configure CustomActivityOnCrash behavior via CaocConfig.Builder

    master

    Use CaocConfig.Builder to customize how the library handles crashes. Key configuration methods include:

    • launchWhenInBackground(int): Defines behavior when the app crashes in the background.
      • CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM: Launch error activity.
      • CaocConfig.BACKGROUND_MODE_CRASH: Launch default system error.
      • CaocConfig.BACKGROUND_MODE_SILENT: Crash silently.
    • enabled(boolean): Enables or disables crash interception.
    • showErrorDetails(boolean): Shows/hides the error details button (stack trace).
    • trackActivities(boolean): Tracks activity lifecycle calls to include in error details.
    • showRestartButton(boolean): Shows a 'Restart app' button instead of 'Close app'.
    • logErrorOnRestart(boolean): Relogs the stack trace to Logcat on restart.
    • minTimeBetweenCrashesMs(int): Minimum time between crashes to prevent crash loops.
    • errorDrawable(Integer): Sets a custom drawable/mipmap resource ID for the error image.
    • restartActivity(Class<? extends Activity>): Sets the specific activity to launch on restart.
    • errorActivity(Class<? extends Activity>): Sets a completely custom error activity class.
    • eventListener(EventListener): Sets a listener for crash/restart/close events (must be a static class or non-anonymous).
    • customCrashDataCollector(CustomCrashDataCollector): Sets a collector for additional crash data (must be a static class or non-anonymous).
  5. Implement a completely custom error activity

    master

    If you provide a custom activity via errorActivity(Class), you must declare it in AndroidManifest.xml with android:process=":error_activity". You can then use the CustomActivityOnCrash static methods to retrieve crash information from the intent:

    • CustomActivityOnCrash.getAllErrorDetailsFromIntent(getIntent()): Returns all details (stack trace, activity log, custom data) as a string.
    • CustomActivityOnCrash.getStackTraceFromIntent(getIntent()): Returns the stack trace as a string.
    • CustomActivityOnCrash.getActivityLogFromIntent(getIntent()): Returns the activity log (if trackActivities was enabled).
    • CustomActivityOnCrash.getCustomCrashDataFromIntent(getIntent()): Returns custom data from your CustomCrashDataCollector.
    • CustomActivityOnCrash.getConfigFromIntent(getIntent()): Returns the library configuration at the time of the crash.

    Crucial Lifecycle Methods To avoid process/Application instance issues, you MUST use these methods to restart or close the app:

    • CustomActivityOnCrash.restartApplication(activity, config): Kills the current process and restarts the app.
    • CustomActivityOnCrash.restartApplicationWithIntent(activity, intent, config): Restarts the app with a custom intent.
    • CustomActivityOnCrash.closeApplication(activity, eventListener): Closes the app and kills the current process.