I want that a new activity should start with some delay on pressing a button. Is it possible to do that , and whats the procedure.
Using an OnClickListener Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity . Button yourButton = (Button) findViewById(R. id.
If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.
Use a postDelayed() call with a runnable that launches your activity. An example code could be
//will care for all posts
Handler mHandler = new Handler();
//the button's onclick method
onClick(...)
{
mHandler.postDelayed(mLaunchTask,MYDELAYTIME);
}
//will launch the activity
private Runnable mLaunchTask = new Runnable() {
public void run() {
Intent i = new Intent(getApplicationContext(),MYACTIVITY.CLASS);
startActivity(i);
}
};
Note that this lets the interface remain reactive. You should then care for removing the onclick listener from your button.
Use This code
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final Intent mainIntent = new Intent(CurrentActivity.this, SecondActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}, 4000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With