1

One of my class has two handlers to send messages at regular interval of time. This class is instantiated in one of the activities. Below is the code:

    public class MyClass {

    private Boolean started = false;
    private Handler handler1 = new Handler();
    private Handler handler2 = new Handler();
    private Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                sendMessage("blah");
            }
        };
    private Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                sendMessage("blah blah");
                if (started) {
                    triggerMessageSending();
                }
            }
        };
    }

   public void startMessageSending(){
        triggerMessageSending();

    }

    private void triggerMessageSending(){
        started = true;
        handler1.postDelayed(runnable1, 500);
        handler2.postDelayed(runnable2, 1000);
    }

    public void stopMessageSending(){
        started = false;
        handler1.removeCallbacks(runnable1);
        handler2.removeCallbacks(runnable2);
    }

}

Here is my activity:

public class MyActivity extends Activity {

private MyClass myClass;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myClass = new MyClass();
        myClass.startMessageSending();

}

 @Override
    protected void onStop() {
        super.onStop();
        myClass.stopMessageSending();
}
}

Everything works fine for the first time. If I press back button and go to the previous activity and come back again(without exiting the app), the sendMessage method is called twice. This becomes three if I do it again. It calls the method as many times as I start this activity without exiting the app. If I exit the app and do this again, it works fine for the first time.

What is the reason for this? Where am I wrong?

NewOne
  • 401
  • 5
  • 19

1 Answers1

0

Not sure about your business logic , but these are the problems that I can see in your code.

  • If both the Runnable are doing the same job ( sendMessage ) what is the point in having two runnables?
  • In second Runnable runnable2, again the same is done. Only difference is check of boolean started which triggers triggerMessageSending() going for a loop

Now MyClass object initiation is done in onCreate with triggerMessageSending() method, which initiates two handlers and then runnables

NOTE: onStop() may or may not get called during activity life cycle . Check this link . I mean its not guaranteed. Do the myClass.stopMessageSending(); in onPause() and check

So your message is called twice.

Community
  • 1
  • 1
Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • I want to send message A and message B alternatively every 500ms. How can I do this in 1 runnable? – NewOne Oct 03 '16 at 08:52
  • Just use a boolean variable and alter it after sending message. if(true) sendOne else sendSecond. The variable should be global and not local to handler. Again if handler is invoked the same global variable is checked and message will be sent . – Sreehari Oct 06 '16 at 06:11