ANR and delays in software
ANR stands for Application Not Responding, and is one of the several nightmares of an Android developer. The Android operating system analyzes the status of apps and threads, and when certain conditions are met it triggers an ANR dialog, blocking the user from any interactive experience. The dialog announces that the application stopped responding, and is not responsive anymore. The user can select whether he/she wants to close the application, or keep waiting until the application becomes responsive again (if this ever happens):
What triggers ANRs and how can I avoid them?
Android systems trigger ANRs in two different situations:
- When there has been no response for an event in five seconds
- If a BroadcastReceiver is still executing 10 seconds after its execution
This happens mostly when an operation is being executed in the UI Thread. In general, any operation expected to be time- or operation-consuming should be performed in a separate thread, keeping the UI Thread available for the user interaction, and only notifying the UI Thread when the operation has been finished. In Chapter 5, Multithreading, we will show some advanced techniques for multithreading and thread communication. There are also different classes that can be used to perform operations in different threads, each of them with its own advantages and disadvantages. In general, when developing an application, remember: ANR dialog appearance frequency is inversely proportional to user satisfaction.