Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Handler?

Tags:

android

I came across this code in a very basic Handler tutorial. The code is working fine but I do not understand why I have to use Handler for progressDialog.dismiss() ??? I removed the Handler part and placed progressDialog.dismiss() in the run() method and it worked fine. So why used Handler???

 import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;


    public class HandlerThread extends Activity{

    private Button start;
    private ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button) findViewById(R.id.Button01);
        start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                fetchData();
            }

        });
    }



    protected void fetchData() {
        // TODO Auto-generated method stub
        progressDialog = ProgressDialog.show(this, "", "Doing...");
        new Thread() {
            public void run() {
                try {

                    Thread.sleep(8000);

                    } catch (InterruptedException e) {

                    }
                      messageHandler.sendEmptyMessage(0);

                    }
        }.start();


    }



    private Handler messageHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            progressDialog.dismiss();

        }
    };
}
like image 683
Syamantak Basu Avatar asked Sep 07 '12 06:09

Syamantak Basu


3 Answers

Why to use Handler in Android?


First: Let us know what is thread:

  • Threads help in multi-tasking
  • Threads can be taught as a mini process running under a main process
  • Threads enable at-least the appearance parallel execution

Second: Let us know about the application thread:-

  • When the android application is first started the runtime system will create a single main thread, this main thread will take care of execution of all the components in android

Android UI-Toolkit is not thread safe

  • As stated there are many components in the android main thread, now suppose one of the components takes a long time for execution then this makes the main thread unresponsive and it will show the application unresponsive
  • Sub-threads cannot directly manipulate the application (main) thread in the android
  • Handler acts as a interface and collects the messages from the sub-threads and update the main application thread one by one as the messages arrive, Thread handlers are implemented in main thread.

Handler class:

  • For the purpose of multithreading we shall use handler class which comes from the package android.os.Handler
  • Each thread is handled by one instance of handler class

Figure

  • From the above figure we can see that Each thread is handled by one instance of the Handler class
  • threads communicate between each other with the help of messages
  • This handler class helps to maintain kind of sync-coordination b/w threads by allowing them to run together achieving multithreading

Instance of handler is made

Handler handlerObject = new Handler();

Final piece on using handler is to use Runnable Interface:

  • handler class utilizes runnable interface to implement multithreading
  • We override run method to execute a thread a specified number of time

Class NameOfClass implements Runnable
{
    Public void run()
    {
        //Body of run method
    }
}

Putting all together

//Create handler in the thread it should be associated with 
//in this case the UI thread
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
    public void run() {
        while(running){
            //Do time consuming stuff

            //The handler schedules the new runnable on the UI thread
            handler.post(new Runnable() {
                //Ex.. using progressbar to set the pogress
                            //Updating the UI is done inside the Handler
            });
        }
    }
};
new Thread(runnable).start();
like image 93
Devrath Avatar answered Oct 20 '22 10:10

Devrath


From the documentation of View:

You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a Handler.

In your example, when you've to call the dismiss() method on the ProgressDialog, as per the above documentation, you must do so from the UI thread. The messageHandler is initialized to an instance of a Handler when the HandlerThread class is instantiated (presumably on the UI thread).

From the documentation of Handler:

Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

So to communicate with the UI thread from your new thread, just post a message to the Handler created on the UI thread.

If you call methods on a View from outside the UI thread, it invokes undefined behaviour, which means, it may appear to work fine. But it's not always guaranteed to work fine.

like image 37
Dheeraj Vepakomma Avatar answered Oct 20 '22 09:10

Dheeraj Vepakomma


The easier the better. Instead of using a handler, you could try to use the following code:

runOnUiThread(
    new Runnable() { 
        public void run() 
        { 

        //Update user interface here

        } 
    }
);

Don't make your life to complicated ;)

like image 38
Marek Avatar answered Oct 20 '22 11:10

Marek