Changing the default Android application crash dialog to its own screen

How to use your own error screen instead of the default crash dialog

Prospective students on the course “Android Developer. Basic” we invite you to attend an open webinar on the topic “Application for Android on Kotlin in 1.5 hours”

For now, let’s share a traditional, useful translation.


I don’t know about you, but I feel very ashamed when I submit my application to a tester or client for review and the application crashes. This is a very unpleasant moment.

“Live as if you will die tomorrow. Learn as if you are going to live forever. “

I don’t like the standard dialog that is displayed when the application crashes. So I found an interesting library that allows you to display your own error message screen instead of a standard dialog box.

Let’s see how to do this.

What do we need?

  1. Kotlin

  2. Android Studio

  3. The same library

After creating a new project add to the file build.gradle(:app) the following dependency:

implementation 'cat.ereza:customactivityoncrash:2.3.0'

Let’s create a layout for our own crash screen. For example, something like this:

Custom crash screen layout
Custom crash screen layout

I downloaded this glitch picture from here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/errorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:fontFamily="sans-serif"
        android:text="@string/ouch_something_went_wrong"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textStyle="bold" />


    <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="https://habr.com/ru/company/otus/blog/530606/@drawable/ic_error_img" />


    <com.google.android.material.button.MaterialButton
        android:id="@+id/restartApp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:text="Restart App"
        android:textAllCaps="false" />

</RelativeLayout>

How to Become a Successful Android Developer

Follow this plan if you want to become a professional Android developer

medium.com

Let’s add some code that monitors the crash event and displays a custom crash screen instead of the standard Android dialog.

package com.example.customerrorscreen

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import kotlinx.android.synthetic.main.activity_error.*

class ErrorActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_error)


        /** you can log the reason of crash by adding this line of code
         *  CustomActivityOnCrash.getStackTraceFromIntent(intent)
         */

        //getting crashing intent
        val config = CustomActivityOnCrash.getConfigFromIntent(intent)

        /**
         * If config is null or not getting an intent simply finish the app
         */
        if (config == null) {
            finish()
            return
        }
        
        if (config.isShowRestartButton && config.restartActivityClass != null) {
            restartApp.text = "Restart App"
            restartApp.setOnClickListener {
                CustomActivityOnCrash.restartApplication(
                    this,
                    config
                )
            }
        } else {
            restartApp.text = "Close App"
            restartApp.setOnClickListener {
                CustomActivityOnCrash.closeApplication(
                    this,
                    config
                )
            }
        }

    }
}

You need to create an application class that will be registered in the Android manifest in order to globally register the crash handling library in the application.

package com.example.customerrorscreen

import android.app.Application
import cat.ereza.customactivityoncrash.config.CaocConfig


/**
 * Created by  Mustufa on 04/11/2020.
 * Email : mustufaayub82@gmail.com
 */
class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()
        CaocConfig.Builder.create()
            .trackActivities(true)
            .errorActivity(ErrorActivity::class.java)
            .apply()
    }
}

Let’s add this class to the file AndroidManifest.xmlusing the attribute name

AndroidManifest.xml
AndroidManifest.xml

Now let’s move on to the file MainActivity.kt and experiment with our new feature. We have to crash the application to see our crash screen. I will add the code causing the application to crash to the method onCreate

var array = mutableListOf<String>()
array[0] = "Hello"
findViewById<TextView>(R.id.textView).text = array[1]

Executing this code will throw an exception IndexOutOfBoundException and the application will crash.

Let’s start the application.

Source code can be downloaded from here.


Learn more about the course “Android Developer. Basic”.

Sign up for an open lesson on the topic “Application for Android on Kotlin in 1.5 hours”.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *