Open In App

What are the Reasons For the Exit in Android Application?

Last Updated : 14 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. getHistoricalProcessExitReasons: takes three parameters in this case,
  2. 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.
  3. 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads