15

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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Abhishek Dhiman
  • 1,631
  • 6
  • 25
  • 38

5 Answers5

26

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?

Juned
  • 6,290
  • 7
  • 45
  • 93
12

Try below line,

handler.removeMessages(0);

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

MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
9

Handler has nothing to be stopped or started. It's just a gateway to post Messages and Runnables onto thread Queue. In your case you are posting a Runnable, which will be run on Handler's thread. You have to stop that Runnable. So, put a check in that for loop to break it.

You can also use removeCallbacks(Runnable r) on Handler to cancel posted Runnable's. But this won't remove those Runnable's which are already running.

S.D.
  • 29,290
  • 3
  • 79
  • 130
2

You should try this onStop():

handler.removeCallbacksAndMessages(calendarUpdater);
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
John Wayne
  • 21
  • 3
1

Make handler a field. Then in your activity, override onStop method and stop handler call back messages like this:

@Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(calendarUpdater);
    }
Karue Benson Karue
  • 957
  • 12
  • 26