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.

How to restore your Android Studio project

If you’ve ever entered the wrong Git command (like the dreaded git reset hard command) and lost all your precious uncommitted work, you’re probably familiar with the pain and the feeling of panic. Well if you were using Android Studio, you just might be in luck. Here I’ll show you a neat little feature Android Studio has that can help you restore your Android Studio project back to a previous point.

In the Project navigation to the left, right click on “app”, then select “Local History” → “Show History”.

You should see the following window.

Here, you can click on the various “checkpoints” that Android Studio made over the course of your project, and you can see changes to individual files and even restore to an older point in time.

Now obviously this isn’t a replacement for a proper file versioning system like Git, but it works just fine if you’re the only one working on a small project as well as for situations like these, so it’s pretty useful to know.

A better way to “git blame” in Android Studio

If you use git in your software projects, you might be familiar with the git blame command. It does pretty much what it sounds like: it lets you blame each line in a file to whoever last made changes to that line. If you’re not familiar with git blame, go ahead and try it on the command line:

$ git blame -- path/to/my/source/file

You should see something this:

Output of git blame

You can see who last modified each line, when the change was made as well as the short hash of the relevant commit. In this example you only see my name because apparently I’m the only one who last modified each line of this file at the same time (which likely means that this was when the file was created and it has not been updated since), but depending on the situation it could mention multiple authors, timestamps and short hashes.

This works great when you’re working from the command line, but it seems a bit too primitive to use when you have the power of an IDE. Shouldn’t Android Studio provide some sort of functionality that works with this?

Introducing Annotate

That’s what they call it in Android Studio (and IntelliJ and possibly other JetBrains IDEs). It does exactly what git blame does – but in a nicer, IDE-friendly way.

Where to find Annotate

If you want to see more information than just the author name and the date modified, just right click on the annotated area, go into View then select what you want to see.

It’s pretty neat for when you want to find someone to blame. 😉