Install Custom Activity On Crash via Gradle
masterAdd the following dependency to your build.gradle file to include the library in your project.
dependencies {
implementation 'cat.ereza:customactivityoncrash:2.4.0'
}repository·master·Indexed 25 days ago
https://github.com/ereza/customactivityoncrashAn 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.
Add the following dependency to your build.gradle file to include the library in your project.
dependencies {
implementation 'cat.ereza:customactivityoncrash:2.4.0'
}If you are using ACRA, you must ensure the following:
onCreate in your Application class (instead of attachBaseContext).alsoReportToAndroidFramework to ensure CustomActivityOnCrash functions correctly.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_explanationcustomactivityoncrash_error_activity_restart_appcustomactivityoncrash_error_activity_close_appcustomactivityoncrash_error_activity_error_detailscustomactivityoncrash_error_activity_error_details_titlecustomactivityoncrash_error_activity_error_details_closecustomactivityoncrash_error_activity_error_details_copycustomactivityoncrash_error_activity_error_details_copiedcustomactivityoncrash_error_activity_error_details_clipboard_labelFirebaseApp.initializeApp(this); inside the onCreate method of your Application class.Be aware of the following constraints when using CustomActivityOnCrash:
UncaughtExceptionHandler after initializing the library that does not call back to the original handler.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();
}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).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.