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);
Did you add a
Logto see ifrun()gets called? I would assume that your handler gets unregistered; after all,postDelayedwill 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
imageView1which I presume is destroyed inonDestroy.You could consider adding a flag that will force the operation to be called immediately in
onDestroy, and/or you could use aTimer. In case of the timer, be sure to not use something likeimageView1after it has been destroyed.