ANR – Application Not Responding, is a system dialog that appears in Android apps when the app becomes unresponsive.
Applications are expected to be responsive. A responsive application is an application that responds quickly to user interactions.
Messages about user interactions, for example when the user touches the screen, are sent to an internal message queue. Whenever the main thread is free it checks the message queue for new messages and processes them. The result might be a button click event, an update to an ongoing animation and so on.
As long as the main thread is busy running other code, it will not process the message queue.
If a user interacts with the device and the message is not processed for about 5 seconds then an ANR dialog will be displayed, asking the user whether they want to continue waiting or to close the app:
Code for above example:
Sub Activity_Create(FirstTime As Boolean) Do While True If False Then Return Loop End Sub
In order to keep the app responsive we need to avoid making too much work on the main thread.
In B4X almost all of our code is run on the main thread, however slow tasks such as network communication are always done on background threads. This is also true for the async database operations ([B4X] SQL with Wait For) and many other cases where the library or framework takes care of executing the task in the background and raising an event on the main thread when done. Overall, it is quite rare to see an ANR dialog with B4X (in release mode).
Related topic: NetworkOnMainThreadException