Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler doesn't execute again on Activity restart

This code is for my splash screen Activity:

    @Override
    protected void onResume() {
    super.onResume();
    handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }, 5000);
}

When I click the device back button ,MainActivity become finish and again SplashActivity comes to up,and again it's onResume method get call, problem is here that handler doesn't execute again and MainActivity doesn't start again after 5 second ! I want it execute again!

what's the problem and what should i do?

thanks for attention.

like image 558
MehDi Avatar asked Jul 12 '26 16:07

MehDi


1 Answers

I modified your code a little bit check this

@Override
    protected void onResume() {
    super.onResume();
   new  android.os.Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }, 5000);
}
like image 195
Rahul Khurana Avatar answered Jul 14 '26 05:07

Rahul Khurana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!