Show a custom crash screen in Android

No one likes it when your app crashes. Many of us as developers do our best to thoroughly test an app before releasing it for everything from random bugs to crashes, and your app crashing after release is probably one of the worst things that could happen post-release. But no matter how much you try to cover every case, there’s always that one weird device with a weird OEM that you missed that managed to crash your otherwise perfect app. It happens.

Problems with the default crash dialog

Well then.

There are a few things wrong with this:

  1. It’s a generic message, and it’s the same for every app that might crash. Your app isn’t special anymore, and all your branding is irrelevant here.
  2. You depend on your user’s Android system to display whatever message it’s programmed to display. In this screenshot I was using an Oreo (API 26) emulator, but different Android ROMs handle this differently. Some show nothing at all.
  3. You can’t tell your user what to do next. Again, Oreo apparently does let you restart your app, but some other versions don’t. You don’t control the message.

So what can you do then? Well the answer is simple: make your own crash screen.

Now bear with me, I know what you’re thinking. “Well maybe your app shouldn’t crash at all.” Let me be clear: by all means, do everything within your power to make sure your app doesn’t crash. Apps aren’t supposed to crash and that’s not something we should be aiming for.

But my point here is, it might still be worth handling, let’s say, the 0.1% probability of your app crashing on some device. It’s probably the last thing you’d want to do with your app, but hey, at least you can rest assured that your users will always see a friendlier message if it does come to the worst.

Displaying a custom crash screen

I’ve used the Custom Activity On Crash library in the past and I’d recommend it to anyone looking to display a custom error activity. It relies on the Thread.setDefaultUncaughtExceptionHandler method to launch a custom activity any time an exception of any sort occurs in your app. It is fully customisable – you can either modify the default activity or even use your own.

You might want to configure .minTimeBetweenCrashesMs() to be zero – it’s the time that must pass after the app crashes before the custom activity is launched and apparently it’s used to prevent crash loops. Problem is, if the time is too long, your users will see the system crash screen instead of your custom activity. I haven’t had a problem with setting it to zero – crash loops, as far as I know, shouldn’t happen unless the error activity itself has an uncaught exception (and I’m sure you can get that one activity right).

If you want, you can of course try implementing your own solution using the exception handler. But do try the library to see if it fits your needs – it most likely does, so you’ll be able save yourself the trouble of reinventing the wheel.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.