Android - Background Processing using Threads

Whenever you make some changes in user-interface and it appears that the screen is updated the same time when you pass that text. But this is more complex procedure. Every changes in user interface goes through a message queue. Whenever a request to change the user-interface goes to the message queue (example : change the text of Button) then android operating system pop the message out of that queue and updates the user-interface.

Now the message queue is processed by main thread or you can also call it user-interface thread (UI thread). Main thread is also responsible for call back methods of activity like onCreate(), onPause(), some click listeners etc.

When main thread has too many work to do then sometimes it delays the response and even it can become unresponsive. So your application might show APPLICATION NOT RESPONDING ERROR.

How to deal with this kind of situation

This simply means that anything which makes your application slow should not be implemented in a main thread. We can execute that operations on another thread.

So if you want to do some extensive task and you also want to update the user interface from background then you can easily do that by creating new thread.

We can accomplish background operations via

  1. Handler.
  2. AsynchTask.

Why it is not recommended to use standard java threads in android

The main problem is the synchronization of normal thread with the main thread. That is if some changes occurred in created thread then it should also reflect in the main thread. But they are not synchronized.

So instead of java threads you can use other other methods of dealing with background processing like using AsynchTask or handlers. Android provides proper API for backgroung processing.