user1125258
user1125258

Reputation:

How to cancel a postDelay Handler

I have a splash screen "intro" activity, that just shows the title of the App. and brief info about the developer and it has a button named "skip". I created a postHandler so that, the main activity starts automatically after 3 seconds. Or, the main activity should starts when the skip button is pressed. If the "skip" button is pressed it should invalidate/remove the postHandled callbacks. But what happens is, even after I press the "skip" button and moved to the main Activity, the main activity starts one more time again, as if, the mHandler.removeCallbacks(mRunnable); is not called. I just want when I press the "skip button to move to the main Activity once and it should not start again when the "skip" button is pressed. Please see the code below, and let me know what is wrong or missing.

Code:

    mHandler = new Handler();
    mRunnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent intent01 =  new Intent(Intro.this, MainMenuActivity.class);
            startActivity(intent01);
            finish();
        }
    };
    ....
    ....
    mHandler.postDelayed(mRunnable, SPLASH_TIME_OUT);

    introButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mHandler.removeCallbacks(mRunnable);
            Intent intent00 = new Intent(Intro.this, MainMenuActivity.class);
            startActivity(intent00);
            finish();
        }
    });

Upvotes: 2

Views: 665

Answers (1)

Rahul Matte
Rahul Matte

Reputation: 1181

Put your logic in onDestroy()

@Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mRunnable);
        Log.d("Callbacks", "Removed");
    }

Upvotes: 1

Related Questions