0

i am using a postDelayed thread, i need to stop that thread when i press back button. PLease anyone help me for that. Thanx in Advance.

This is my thread:

music1.postDelayed(new Runnable() {
    public void run() {
          music1.setVisibility(View.VISIBLE);
        animationmusic1();
        holemusic1();

    }
}, 10000);
athulya k
  • 145
  • 2
  • 10

3 Answers3

7

http://developer.android.com/reference/android/view/View.html#removeCallbacks(java.lang.Runnable)

Declare Runnable runnable as a instance variable. Then

runnable =new Runnable() {
    public void run() {
         music1.setVisibility(View.VISIBLE);
         animationmusic1();
         holemusic1();
    }
    };
music1.postDelayed(runnable,10000);

Then in onPause

music1.removeCallbacks(runnable);
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2

add an onBackPressed() in YourActivity :

/** (non-Javadoc)
     * @see android.app.Activity#onBackPressed()
     */    
@Override
    public void onBackPressed() {
        music1.removeCallbacks(runnable);
        YourActivity.super.onBackPressed();
    }
ahmed_khan_89
  • 2,755
  • 26
  • 49
0
handler.removeCallbacks(runnable);
  • 4
    Welcome to [so]. When answering questions, please make sure that your answer isn't already covered by another one, especially since the answer was posted long ago. Thanks! – Qantas 94 Heavy May 25 '14 at 09:59