Javed Salat
Javed Salat

Reputation: 526

How to redirect from one activity to another after delay

I am developing an android application. In this application, I want to transition from one activity to another activity automatically after 4 seconds. I don't know how to do this without a button.

Upvotes: 13

Views: 36800

Answers (3)

Riddhi Shah
Riddhi Shah

Reputation: 3122

Add the code in your oncreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
     Handler handler=new Handler();
     handler.postDelayed(new Runnable() {
         @Override
         public void run() {
                Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
                startActivity(intent);
         }
    },4000);
}

Upvotes: 2

jyotiprakash
jyotiprakash

Reputation: 2086

This is how you can proceed:

int timeout = 4000; // make the activity visible for 4 seconds

Timer timer = new Timer();
timer.schedule(new TimerTask() {

    @Override
    public void run() {
        finish();
        Intent homepage = new Intent(Activity1.this, Activity2.class);
        startActivity(homepage);
    }
}, timeout);

Upvotes: 35

Daniel Chow
Daniel Chow

Reputation: 201

You can add a Handler in you activity, like:

private Handler handler = new Handler();

Then in your onCreate() method of activity, you can call:

handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                startActivity(yourIntent);
            }
        }, 4000);

Upvotes: 0

Related Questions