Developers can write code for Android apps with an optimization that allows them to scale through most performance tests but may not be able to process input swiftly, causing the app to “hang” for some time. The android system can detect apps that take too long to respond and warns the user against such apps by displaying the dialog box saying your application is not responding, thus giving the user the option of closing the app.
When it comes to responsiveness, the “Application Not Responding” (ANR) dialog is the worst thing that can happen to your app and may make users stop using your app. This makes it important to create optimization that allows your app to be more responsive, so the android system does not display the ANR dialog to the user. This article will tell you about how an android system declares an app non-responsive and what you can do to avoid it.
If the android system detects that an application can’t respond to user input, it will display the ANR dialog. For example, if the app spends a lot of time in the process of building a complex in-memory structure or calculating the next move in a game using the UI thread. It is critical to make sure that these calculations are efficient, but even the most efficient code can still take a lot of time to run.
The activity manager and window management system services are involved in monitoring application responsiveness in android systems. If the system detects any of the following conditions in your app, it will display the ANR dialog.
- If there is no response to an input by the user (for example, a keypress) for more than 5 seconds.
- If a Broadcast receiver has not finished executing in more than 10 seconds.
Normally, Android apps run in a single thread by default; this is the UI thread. This tends to affect responsiveness because the UI thread is involved in rendering UI components, and if this thread (which also happens to be the main thread) is involved in computationally expensive activities that may take more than 5 seconds to run, it puts too much strain on the UI thread. This would lead to the android system concluding that the code has frozen, and ANR dialog would be displayed. To avoid this, any potentially complex operation that your app has to run shouldn’t be done on the UI thread. A separate worker thread should be created, and all the work should be done there.
● You can use strict mode to help you detect long-running processes that you may not know that you’re running on the UI thread.
● Performance tools like systrace or traceview can also be used to detect jams in your app’s responsiveness.
These are just a few tips that can help you as a developer in the optimization of your app’s responsiveness and reduce the time your app takes in starting up and running processes.