Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Handler in Android

In my application I have created a calendar with Gridview and in that Gridview I am displaying dates and some availability of events with the help of Imageview and to do this I have created a handler.

Now I want to stop the handler.

MainActivity.java

// inside oncreate

Handler handler = new Handler();
refreshCalendar();

// outside oncreate

public void refreshCalendar() { 
    calAdapter.refreshDays();
    calAdapter.notifyDataSetChanged();
    handler.post(calendarUpdater);
    calTitle.setText(android.text.format.DateFormat.format("MMMM yyyy", cal));
}
public Runnable calendarUpdater = new Runnable() {

    @Override
    public void run() {
        items.clear();
        allData = new ArrayList<HashMap<String,String>>();
        allData.clear();
        allData = db.showAllEvents();

        String currentDate = (String)android.text.format.DateFormat.format("MM/yyyy", cal);
        for(int i=0; i<allData.size(); i++)
        {
            String date[] = allData.get(i).get("date").split("/");
            String md[] = currentDate.split("/");
            if(date[1].equals(md[0]) && date[2].equals(md[1]))
            {
                items.add(date[0]);
                System.out.println("dates: "+date[0]);
            }
        }
        calAdapter.setItems(items);
        calAdapter.notifyDataSetChanged();
    }
};

Please tell me how and where I should disable this thread.

like image 531
Abhishek Dhiman Avatar asked Jan 28 '13 07:01

Abhishek Dhiman


People also ask

How do I get rid of handler on Android?

removecallback and handler = null; to cancel out the handle just to keep the code clean and make sure everything will be removed.

How do you stop handler threads?

postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(MapsActivity. this,MapsActivity. class); startActivity(i); finish(); } }, TIME_OUT); Then you can use Handler#removeCallbacksAndMessages to remove this or any callback.

What is a handler in Android?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . 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 a Looper .


2 Answers

You can use this to stop that runnable

handler.removeCallbacks(calendarUpdater);

removeCallbacks(Runnable r) :Remove any pending posts of Runnable r that are in the message queue.

Edit

You can organize your code like this

In your onCreate() of MainActivity.java

Handler handler = new Handler();
refreshCalendar()

//outside  oncreate 

public void refreshCalendar() { 
    calAdapter.refreshDays();
    calAdapter.notifyDataSetChanged();
    startRepeatingTask();
    calTitle.setText(android.text.format.DateFormat.format("MMMM yyyy", cal));
}

public Runnable calendarUpdater = new Runnable() {

    @Override
    public void run() {
        items.clear();
        allData = new ArrayList<HashMap<String,String>>();
        allData.clear();
        allData = db.showAllEvents();

        String currentDate = (String)android.text.format.DateFormat.format("MM/yyyy", cal);
        for(int i=0; i<allData.size(); i++)
        {
            String date[] = allData.get(i).get("date").split("/");
            String md[] = currentDate.split("/");
            if(date[1].equals(md[0]) && date[2].equals(md[1]))
            {
                items.add(date[0]);
                System.out.println("dates: "+date[0]);
            }
        }
        calAdapter.setItems(items);
        calAdapter.notifyDataSetChanged();
        handler.postDelayed(calendarUpdater,5000); // 5 seconds
    }
};

void startRepeatingTask()
{
    calendarUpdater.run(); 
}

void stopRepeatingTask()
{
    handler.removeCallbacks(calendarUpdater);
}

Now you can just call startRepeatingTask() to posting message and to stop use stopRepeatingTask()

Inherited from following link

Repeat a task with a time delay?

like image 110
Juned Avatar answered Oct 05 '22 04:10

Juned


Try below line,

handler.removeMessages(0);

Remove any pending posts of messages with code what that are in the message queue.

like image 42
MuraliGanesan Avatar answered Oct 05 '22 02:10

MuraliGanesan