0

At the beginning, I would like to apologize for my bad English.

There is my problem:

Below timer is showing message at every 3 second , but when i getting out from the program (using back button) the message still pop out.

 public Runnable mUpdateTimeTask = new Runnable(){        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(3000);}
                 catch (Exception e) {
                    // TODO: handle exception
                }
            mHandler.post(new Runnable(){
        @Override
         public void run() {
                            Toast.makeText(getApplicationContext(), "Testing 3 Seconds", Toast.LENGTH_LONG).show();
                  }
              });
            }
        }

};

i already use mHandler.removeCallbacks(mUpdateTimeTask); still can't stop it.

Thanks in advance.

2 Answers2

0

The call has been made from a runnable thread class, which is still active when Back button is pressed. It will pop messages until the application destroys. Instead, use this:

    handler.removeCallbacksAndMessages(null);

In the docs for removeCallbacksAndMessages it says. here is the link: developer website documentation link

"Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed."

0

I recently had the same problem and after searching a lot, I found the solution here.

handler.removeCallbacksAndMessages(null);

would only stop pending messages (Runnables). It won't work for the currently running runnable. You will have to write your own class to stop the currently running runnable.

Community
  • 1
  • 1