sohaib rahman
sohaib rahman

Reputation: 301

How to display an activity automatically after 5 seconds?

In my application I have created a splash screen type of thing in Android. It should remain for 5 seconds.

My problem is how do I display another activity automatically after 5 secs?

The splash screen doesn't have a button, rather it should display another activity automatically after 5 seconds without the click of a button.

Upvotes: 21

Views: 49529

Answers (4)

Ayaz Alavi
Ayaz Alavi

Reputation: 4835

TimerTask task = new TimerTask() {

            @Override
            public void run() {
                Intent intent = new Intent(SplashScreen.this, MainMenu.class);
                startActivity(intent);
                finishscreen();
            }
        };
        Timer t = new Timer();
        t.schedule(task, 5000);

and

private void finishscreen() {
        this.finish();
    }

Upvotes: 9

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

This can also be done using android CountDownTimer class.

See this example for 5seconds delay.

new CountDownTimer(5000, 1000) {
    public void onFinish() {
         Intent startActivity = new Intent(ThisActivity.this,ActivityToStart.class);
         startActivity(startActivity);
        finish();
    }

    public void onTick(long millisUntilFinished) {
    }

}.start();

You may also need to define your parent activity in AndroidManifest.xml file,

<activity
      android:name=".ActivityToStart"
      android:label="Back"
      android:parentActivityName=".MainActivity" >

      <!-- Parent activity meta-data to support 4.0 and lower -->
      <meta-data
           android:name="android.support.PARENT_ACTIVITY"
           android:value=".MainActivity" />
</activity>

Upvotes: 1

kzotin
kzotin

Reputation: 5365

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final Intent mainIntent = new Intent(LaunchActivity.this, HomeActivity.class);
                LaunchActivity.this.startActivity(mainIntent);
                LaunchActivity.this.finish();
            }
        }, 5000);

Upvotes: 60

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34823

You can use thread here
For example

// thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(500);
                        if(_active) {
                            waited += 500;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // start your activity here using startActivity
                    stop();
                }
            }
        };
        splashTread.start();

Upvotes: 1

Related Questions