0

I am new in android, i use to work with asynk task mostly, there is Handlers also, when read differences between thread, asynk task and Handler i confuse why should not use only Handler if handler can do every thing just for one difference i.e. Asynk task initialize on UI thread and Handler can initialize from any. Please some give me the situation when to use Asynk task and when not and why? same with Handler?

Electro
  • 395
  • 4
  • 16

2 Answers2

0

AsyncTask is used to run in background without interrupting the UI thread (main thread), Handlers run on the main thread and are good options to communicate the UI components from another thread for purposes like UI update operations but if you use it to do a expensive process on, your main thread will be blocked until the job is done. I personally use Handlers as a message management part to keep up my UI components considering the app events and use a AsyncTask to run a process in background along a Fragment or Activity lifecylce.

Farshad
  • 3,074
  • 2
  • 30
  • 44
0

You are mistaking with handler. Handlers are used to communicate between two threads. To achieve function what Asynctask is used to achieve you have to use handler and thread together as thread will be used to do background processing while handler will be used to update UI accordingly. While both of this tasks can be performed by asynctask itself.Hope this clears a thing a bit. Also you can refer this link for further clarifications Handlers and Thread

Community
  • 1
  • 1
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84