Normally when programming a mobile application, we often encounter apps crashing, which is when the current application cannot operate (force close). But there is another status that is less serious: Application Not Responding (ANR). Why is it less serious because your application can continue to be used normally after waiting? The question is how to minimize ANR errors? Let’s find out below.
What is ANR?
When the user interface flow of an Android app is blocked for too long, the “Application Not Responding” (ANR) error is triggered. If the application runs in the foreground, the system displays a dialog box to the user as shown in the figure. ANR occurs when some long operation takes place in the main thread. This is the event loop stream, and if it’s busy, Android can’t handle any more GUI events in the app.
How to prevent an ANR?
Stop doing heavy tasks on the main thread. Instead use worker threads such as Intent Service, Async Task Handler, or another Thread simply.
Detecting where ANRs happen is straightforward if it’s a permanent block (deadlock acquiring some locks for instance), but harder if it’s just a short-lived delay. First, re-evaluate your code and appearance for vulnerable spots and long-running operations. Error-causing examples may include usage:
- Service.onCreate()/Service.onStartCommand()/Service.onBind() in seconds, thread sleeps, blocking operations from within the event thread and other use Context.startForegroundService() to start a new service on the foreground, but that service doesn’t call startForeground().
- If BroadcastReceiver has not completed its execution within a certain period of time. If the app has any activity in the foreground, this timeout is 5 seconds.
- If JobService does not return from JobService.onStartJob() or JobService.onStopJob() within a few seconds, or if a user-requested command starts and your application does not call JobService.setNotification() within a few seconds after JobService.onStartJob() is called. For apps targeting Android 13 and below, ANR errors are inactive and not reported to the app. For apps targeting Android 14 and above, ANR errors must be visible and reported to the app.
Resolutions
Use the following to diagnose and fix the problem:
- You should make sure these all happen in separate threads. If nothing seems to matter, use Android Resource Analyzer and enable the thread view. This shows all the threads in your application like the trace you have. Reproduce the ANR and refresh the most thread at an equivalent time.
- Android vitals can help improve your app’s performance by alerting you, via the Play Console, when your app is exhibiting excessive ANRs.
- The older way might be to use DDMS and enable stream views.
- You can use ADB to gather logs and have a look.
- You can also add Crashlytics from Firebase to catch hold of ANRs when they occur in your app and get their frequency.
Conclusion
Encountering errors like crashes and Application Not Responding (ANR) issues is common in mobile application development. Understanding ANRs and implementing strategies to minimize them is essential for maintaining a positive user experience. Diagnostic tools such as Android Resource Analyzer, Android Vitals, and Crashlytics provide valuable insights into app performance and help pinpoint ANR issues. By using these tools and monitoring performance metrics, developers can create responsive and user-friendly mobile applications.
In conclusion, by adopting best practices, implementing effective strategies, and utilizing diagnostic tools, developers can minimize ANR errors and ensure their apps deliver a smooth user experience.
To gain more insightful information, check out our blog!
Tien Tran, Android Developer