How To Replace The Standard Crash Dialog With A Custom Error Screen? (Android)

Emirhan Kolver
4 min readSep 24, 2022

--

I’ve converted all of this to single dependency check out the Project’s Github Page

Errors… Our apps occasionally crash for causes we never expected, despite the precautions we take against all of them. So, instead of always displaying the user with this boring crash dialog during the error, wouldn’t it be wonderful if we displayed our own crash screen?

What we’re going to do is pretty simple. First we will create ourselves a Crash Activity and App Class. Then, with a single line that we will add to the App Class, we will start our own Crash Activity whenever our application crashes.

So.. Let’s start from the hardest part. Let’s create ourselves a class named
GlobalExceptionHandler and it will need three main parameters. Also it have to be extended by Thread.UncaughtExceptionHandler class

The first look at our class

We’re gonna start the activityToBeLaunched whenever our app crashes but we have to modify the Intent a little bit to launch our activity so lets create a private function named launchActivity

The Private Function

Let’s head back to uncaughtException method and call our new method

Final look of the uncaughtException method

defaultHandler.uncaughtException(p0, p1) function’s called whenever any kind of exception occurs while starting the Activity. In this situation default error dialog will show up instead of activityToBeLaunch

Gson Dependency

We need it to lighten our work. Add the line below to your build.gradle(App) file

implementation(“com.google.code.gson:gson:2.9.1”)

Last step is creating a companion object and creating two more methods inside.

Companion object of GlobalExceptionHandler

initalize: Most important function. It has to be run at OnCreate function at Application class. It will replace the default ExceptionHandler to our class.

getThrowableFromIntent: It’ll retrive Throwable data from the activity we’re gonna use.

Crash Activity

Let’s continue to our work by creating our Activity that will show after our app crashes.

Options under the new menu
Declaring name for the Crash Screen

We created our Crash Activity. Now let’s make the design.

Preview of Crash Activity

You can check out project with all contents inside from here

Our design is complete. Next step is giving some actions to these buttons. Let’s give them…

We gave the actions to the buttons and last important thing we’re gonna do is seprating the Main Application process with the Crash Activity by adding single line to manifest. So whenever our application’s process is crashed the Crash Activity will be not infected by that error

Test Activity

After completing our error activity, let’s create an activity that has only a simple design and will only cause the application to crash. I will edit the Main Activity we already have.

First of all, I will use a simple TextView and Button as a design.

The preview of Main Activity

In the code part, I will throw an error when the button is pressed.

MainActivity.kt

Creating the App class

If you don’t have an application class at your project. Open your AndroidManifest file and get inside of Application tag and add the android:name line to your file

It’s showing App name as red as expected.
Click the bulb and select Create class ‘App’
Let’s call our GlobalExceptionHandler.initalize method

Everything is done!

Let’s launch our app and see the results.

With GlobalExceptionHandler / Without GlobalExceptionHandler

Our GlobalExceptionHandler class is working as expected! You can also see the Throwable data at logs.

Thanks for reading!

--

--