Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler.postdelayed

Tags:

android

I am using handler.postDelayed method to create some delay for some animation stuff.

With that i am playing some song as well using Mediaplayer. User can exit this action class by clicking next. But on next screen the same song is continuing even though i called stop method in the next button's onclicklistener.

Is it due to the timedelay that is added which gets executed after the next activity is loaded. Any idea?

handler.postDelayed(new Runnable() {
        public void run() {
            mp = MediaPlayer.create(getApplicationContext(), R.raw.num2);
            mp.start();
            imageView1.setImageResource(R.drawable.countcat2);
        }
    }, 2000);
like image 308
nasaa Avatar asked Nov 24 '25 19:11

nasaa


1 Answers

Did you add a Log to see if run() gets called? I would assume that your handler gets unregistered; after all, postDelayed will wait until the looper kicks in again.

I assume your animation is faster than those 2000ms? You wouldn't be able to have your handler called anyway after your activity is gone, it's accessing imageView1 which I presume is destroyed in onDestroy.

You could consider adding a flag that will force the operation to be called immediately in onDestroy, and/or you could use a Timer. In case of the timer, be sure to not use something like imageView1 after it has been destroyed.

like image 160
EboMike Avatar answered Nov 26 '25 10:11

EboMike