I don’t know about you but I feel very embarrassed when I deliver an app to tester or client for testing and it get crash. That moment is very painful for me.
I personally don’t like that crash alert dialog that appears when my app gets crash. So I found an interesting library that will show an error screen instead of an error dialog.
Let’s get started.
What I’m going to use:
- Kotlin
- Android Studio
- Library
After creating a new project add this dependency in your build.gradle(:app)
.
implementation 'cat.ereza:customactivityoncrash:2.3.0'
I am making my custom crash layout. Like this:
I downloaded this error image 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="@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> |
Let’s do some coding part to catch the crash event and show custom screen instead of android default 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 | |
) | |
} | |
} | |
} | |
} |
Now you need to make an application class that will register in the android manifest to register crash library globally in-app.
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() | |
} | |
} |
Add this class in AndroidManifest.xml
using name
attribute.
Now moving towards MainActivity.kt
to see this in action. We need to crash our app to see the crash screen. I am implementing a crashing code in my onCreate
method.
var array = mutableListOf<String>()
array[0] = "Hello"
findViewById<TextView>(R.id.textView).text = array[1]
This will crash my app as this code will generate IndexOutOfBoundException
.
Let’s run the app.
Download Source code from here.
Top comments (0)