Reputation: 526
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
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
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
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