문제

In my app i set the splash screen timer to 5 sec and later on think that 5 sec is too long so i change it back to 1 sec and my splash screen doesn't seen on the screen and keep me waiting for more than 5 sec i couldn't find what is wrong so here is my Splashscreen code

    public class Splash extends Activity
{
    private Timer_Countdown timer_Countdown = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_screen);
        timer_Countdown = new Timer_Countdown(5000, 1000);
        timer_Countdown.start();
    }

    class Timer_Countdown extends CountDownTimer
    {
        public Timer_Countdown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            timer_Countdown.cancel();
            Intent startIntent;
            startIntent = new Intent("android.intent.action.MAINMENU");
            startActivity(startIntent);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
    }

And one last thing if I change it back to 5 sec it shows up on the screen again.

도움이 되었습니까?

해결책

Why you are using this much of code just to use splash screen. Make it simple, you can use below code.

public class Splash extends Activity {

    Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        timer.schedule(new TimerTask() {
               public void run() {
                   Intent intent = new Intent(Splash.this, NewActivity.class);
                   startActivity(intent);
                               finish();
               }
            }, 2000);
    }
}

다른 팁

You can use Handler also

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            startActivity(new Intent(SplashActivity.this, YourNewActivity.class));
            finish();

        }
    }, 3000);

or Using Timer with Timer Schedule

public class Splash extends Activity {

 Timer t= new Timer();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    t.schedule(new TimerTask() {
           public void run() {
               Intent n= new Intent(Splash.this, YourNewActivity.class);
               startActivity(n);
           }
        }, 3000);
 }
 }

Use this instead of Timer

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                //code for starting new activity

            }
        }, 5000);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top