Posts

Showing posts with the label AntiPattern

Antipattern: freezing the UI with a Service and an IntentService

Image
The worst thing that can happen to your app's responsiveness is an "Application Not Responding" (ANR) dialog. In my previous posts I described how to freeze the UI with a Broadcast Receiver and with a Async Task . In this post I'll freeze the UI with a Service and with an IntentService . A Service is an application component that can perform long-running operations in the background. We should read official doc very carefully. A Service is not a separate process. The Service object itself does not imply it is running in its own process A Service is not a thread . It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors) It should be simple...if you do use a service, it still runs in the main thread of its hosting by default, so you should still create a new thread within the service if it performs intensive or blocking operations. The standard pattern for implementing a Service is to create and run a new ...

Antipattern: freezing the UI with an AsyncTask

Image
The worst thing that can happen to your app's responsiveness is an "Application Not Responding" (ANR) dialog. In my previous post I described how to freeze the UI with a Broadcast Receiver. It is very important to understand which methods run in Main UI Thread. In this post we can find an example OF WHAT NOT TO DO WITH AN ASYNCTASK . We use an AsyncTask to execute background tasks in order to avoid the UI to become unresponsive, but not all methods run on the background thread. In our AsyncTask class we must override doInBackground() which is invoked on the background thread. There are three other optional methods each of which is invoked from the UI thread : onPreExecute – this is called before doInBackground() onProgressUpdate – this is called while doInBackground() is executing onPostExecute – this is called after doInBackground() If your app ties up the UI thread for more than 5 seconds, Android will throw up the Application Not Responding (AN...

AntiPattern: freezing a UI with Broadcast Receiver

Image
The worst thing that can happen to your app's responsiveness is an "Application Not Responding" (ANR) dialog. In android guidelines we can read: In any situation in which your app performs a potentially lengthy operation, you should not perform the work on the UI thread , but instead create a worker thread and do most of the work there. This keeps the UI thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen. It's very important to understand what is Main Thread . When an Android app is launched, the system creates a thread for the application which is known as the main thread. If you run up any project under the debugger in Eclipse you will see the main thread. If your app ties up the UI thread for more than 5 seconds or a BroadcastReceiver does not complete within 10 seconds, Android will throw up the Application Not Responding (ANR) dialog which gives the user the opportunity t...