What are the Reasons For the Exit in Android Application?
Last Updated :
14 Sep, 2022
Developing Android Apps can be a tedious job, and frustrating too if our app is exiting unknowingly! In order to log exits in Android, we rely on a number of third-party libraries. With Android R, we can now log the exits in our app, which can assist us in resolving any issues that may arise. In this Geeks for Geeks article, we will discuss how we can log the user's exits from our application. Let's start with a discussion of the different types of crashes and exits that might occur in our application. There could be a crash due to an exception, or there could be an ANR that causes your app to crash and exit users. Users may also exit the app on purpose after they have finished using it.
Consider the following scenario: we're using WhatsApp and want to record all of the actions that occur when a user exits the app. It could be a crash or an exception, or it could be a user action to exit the app. So, in order to obtain all of the information, we will employ the ActivityManager. But first and foremost,
What exactly is ActivityManager?
ActivityManager provides information about activities, services, and the containing process, as well as interacts with them. This class's methods provide us with information that we can use to debug our application.
Kotlin
class gfgActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.gfg_main_act)
}
}
and then to initialize the activity we:
Kotlin
val am = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
Now we look closely at the following parameters to get better insights:
- getHistoricalProcessExitReasons: takes three parameters in this case,
- Name of the package: We passed the package name of our application's PID here: We used 0 as the default to get all of the reasons.
- The length of the list of exit reasons. As a result, we will get a single item in a list of types ApplicationExitInfo.
As it provides historical data, the gfgList returns the exit history rather than the exit information in real-time. Let's start by configuring the gfgList to return the cause and reason of our crashes.
Kotlin
if (gfgList.isNotEmpty()) {
val lastExitInformation: ApplicationExitInfo = gfgList.first()
}
The gfgList returns the exit history rather than the exit information in real-time because it provides historical data. To begin, configure the gfgList to return the cause and reason of our crashes.
Include the following code in our onCreate() function:
Kotlin
Log.i(TAG, "Exit Reason: ${lastExitInformation.reason}")
Log.i(TAG, "Time of Exit: ${lastExitInformation.timestamp}")
lastExitInformation.description?.let {
Log.i(TAG, "Some Vague Desc: ${it}")
}
Case 1: When we close the app from the recent apps tray and then reopen it, we see the following log,
gfgActivity: Exit Reason: 10
gfgActivity: Time of Exit: 732973191
gfgActivity: Some Vague Desc: removed task
Here, Reason code 10 denotes REASON USER REQUESTED. We also received a timestamp in our log, as well as the description remove a task, because we removed the app from the recent app's tray.
Case 2: When our application crashes. So, first, let's add a crash to our application
We'll make a button out of it, and when the textView is clicked:
lateinit var myGfgButton: Button
We will call the button's click listener, as shown below. This would cause our application to crash because the button is not mapped to any widget in XML.
gfgTextView.setOnClickListener {
myGfgButton.setOnClickListener { }
}
When we reopen the app, it will generate a log.
MainActivity: Exit Reason: 14
MainActivity: Time of Exit: 2371217628126
MainActivity: Some Vague Desc: crash
The reason code is 4, which corresponds to REASON CRASH from the above table, and the description also includes the word crash.
GeekTip: If any exceptions occur, the reason code is REASON CRASH.
Case 3: Let us use the System to terminate the JVM. exit()
So, when we click textView, we'll add System.
like exit(),
gfgTextView.setOnClickListener {
System.exit(0)
}
The app will be terminated as a result of this action. When we reopen the app, it will generate a log that looks like this:
MainActivity: Exit Reason: 1
MainActivity: Time of Exit: 92738172192
There is no description here, but the reason code is 1, which corresponds to REASON EXIT SELF. This means it self-destructed.
Case 4: When our application is denied
So, to generate an ANR, we will run an infinite loop on textView click using,
gfgTextView.setOnClickListener {
var spandan = 0
while (true) {
spandan++
}
}
So, if the app receives an ANR, the log generated would be,
MainActivity: Exit Reason: 6
MainActivity: Time of Exit: 27162871216
The reason code, in this case, is 6, which stands for REASON ANR. These are a few examples of where we can log our application exits.
GeekTip: In this article, the maximum size is set to 1. If we change it to a number greater than one, let's say ten. It will return a list of the last ten exit reasons. We update the code to set the maximum size to ten.
Conclusion
This is how we can use getHistoricalProcessExitReasons to get the reasons for the app's exit, which can be very useful for developers to understand the app's user behavior or to check for crashes.
Similar Reads
Android Applications and Their Categories
Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
5 min read
How to Build a SOS Mobile Application in Android Studio?
The SOS applications are basically advanced emergency apps that can rescue you and/or your loved ones if you and/or they find themselves in a life-threatening emergency situation and need immediate assistance. When you need some personal assistance, you can actually turn on your phone and can call o
15+ min read
How to Make a Phone Call From an Android Application?
In this article, let's build a basic android application that allows users to make phone calls directly from the app. This is accomplished with the help of Intent with action as ACTION_CALL. Basically Intent is a simple message object that is used to communicate between android components such as ac
5 min read
How to Change Application's Starting Activity in Android?
Many times while building an android application. We have to change the default activity of our application to another activity that we have created within our android studio project. So that we can set that custom activity to the starting activity of our application. In this article, we will take a
2 min read
How to Check if Application is Installed in Your Android Phone and Open the App?
In this article, we are going to check if a particular app is installed on our phones or not. If yes then we will be having an option to open the app. Else it will show a toast message saying Not available. So here we are going to learn how to implement that feature. Note that we are going to implem
3 min read
How to Finish All the Previous Activities in an Android Application?
Android applications are having so many activities present inside them for different functionalities. When a user performs some action within the application he will be navigated from one screen to another. When the user is navigated to the next screen the previous activity remains open in the stack
4 min read
Difference Between Singletons and Application Context in Android
Android is a mobile operating system based on the Linux kernel and developed by Google. It is designed for smartphones, tablets, and other mobile devices. Android is open-source, meaning that developers can modify and customize the operating system. It is the most widely used mobile operating system
3 min read
Android Package Name vs Application ID
As an Android Developer, you should know that the Package Name that you choose for your app is very important. We are referring to the Package Name of the application which we declare in the Manifest.xml rather than the Java package name (although they will be identical). But there is a difference b
3 min read
How to Launch an Application Automatically on System Boot Up in Android?
In many devices, we need our App to launch automatically when the device is started. In this article, we see how to add the auto-launch functionality to the App on the system boot. System Boot is а stаrt-uÑ sequenÑe thаt stаrts the оÑerаting system оf our device when it is turned оn. This annoying f
3 min read
How to Solve OutOfMemoryError in Android Application?
OutOfMemoryError, or simply OOM, is a problem that every Android developer has encountered. If you haven't seen an OOM in your Android application yet, you will in the near future. Memory leaks cause the OutOfMemoryError to occur in Android. So, in order to remove the OutOfMemoryError, all you need
9 min read