-2

I am implementing a splash screen as per the tutorial here, however the splash screen disappears very quickly almost instantly. What would be the best way to include a timer to only start the new activity after for example 1 second. My Splash screen Activity file looks as follows:

    public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
} 

The approach relies on a drawable and style resource.

the_big_blackbox
  • 1,056
  • 2
  • 15
  • 35
  • You can use `handler.postDelayed` and in it set a timeout which you want and call a method to start a new activity. Basically the answer given below :D – Vucko Jun 30 '16 at 11:12
  • http://stackoverflow.com/questions/8958459/how-to-show-a-splash-screen-for-3-seconds-on-android – Miles Jun 30 '16 at 11:12
  • http://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen – Ashish Ranjan Jun 30 '16 at 11:15

4 Answers4

1

The simplest way would be to use Handler:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, DateUtils.SECOND_IN_MILLIS);
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
1

Code for Splash :-

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

public class Splash extends AppCompatActivity {
    // Splash screen timer

    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);

        // Showing splash screen with a timer.
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // Start your application main_activity
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);

                // Close this activity
                finish();
            }
        }, SPLASH_TIME_OUT); // Timer
    }
}
0
public class SplahActivity extends Activity {

    public static final int Tick = 1000;
    public static final int Complete = 5000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new CountDownTimer(Complete, Tick) {

            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                //start Activity
               Intent intent = new Intent(this, MainActivity.class);
               startActivity(intent);
               finish();
            }
        }.start();
    }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

Use this

timerHandler = new Handler();
getmi_runnable = new Runnable() {
@Override
public void run() {
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    startActivity(intent);
    finish();
  }
};
timerHandler.postDelayed(getmi_runnable, 4000L);
Ram Koti
  • 2,203
  • 7
  • 26
  • 36