Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect from one activity to another after delay

Tags:

android

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.

like image 410
Javed Salat Avatar asked Feb 25 '12 13:02

Javed Salat


2 Answers

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);
like image 81
jyotiprakash Avatar answered Oct 27 '22 12:10

jyotiprakash


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);
}
like image 42
Riddhi Avatar answered Oct 27 '22 13:10

Riddhi