Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a different activity with some delay after pressing a button in android?

Tags:

android

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.

like image 841
Prachur Avatar asked Feb 10 '11 14:02

Prachur


People also ask

How do I make a button go to another activity?

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.

How do I start the same activity again on android?

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.


2 Answers

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.

like image 171
mad Avatar answered Oct 03 '22 06:10

mad


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);
like image 23
Milan Shukla Avatar answered Oct 03 '22 07:10

Milan Shukla