4 Jul 2015

What is ANR? How to avoid application ANR?

ANR stands for A pplication N ot R esponding.
Android systems shows this dialog, if application is performing too much of task on main thread and been unresponsive for a long period of time.
ANR  is not exactly an error.It is shown when your application is very sluggish and takes a lot of time to respond making the user wait.The user won't appreciate if your application makes him wait for a long time so the android framework gives the user an option of closing your application.
This occurs when you are doing long running operations on the main thread and the system can't process user interactions during this period since the main thread is blocked.The solution is to do the heavy operations in a worker thread and keep the main thread free.

An ANR will occur if you are running a process on the UI thread which takes a long time, usually around 5 seconds. During this time the GUI (Graphical User Interface) will lock up which will result in anything the user presses will not be actioned. After the 5 seconds approx has occurred, if the thread still hasn't recovered then an ANR dialogue box is shown informing the user that the application is not responding and will give the user the choice to either wait, in the hope that the app will eventually recover, or to force close the app.
ANR in application is annoying to user. It can be caused due to various reasons. Below are some of the tips to avoid ANR
  • Perform all you long running network or database operation in separate thread
  • If you have too much of background tasks, then take it off the UI thread. You may use IntentService
  • Server not responding for longer period can be guilt for ANR. To avoid always define HTTP time out for your all your webs service calls.
  • Be watchful of infinite loops during your complex calculations

No comments:

Post a Comment