Search code examples
androidtimedelay

android time delay between views


I'm not sure, what is stopping this from working. I have the code setup to cause a 3 second time delay but the View isn't working, it stays black and then after 3 seconds switches to the next screens. I think, I am doing the time delay and something hasn't been called within Android to display the layout...

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);     
    start = System.currentTimeMillis();
    setContentView(R.layout.team);
}

protected void onStart()
{
    super.onStart();        
    while(game)
    {
        now = System.currentTimeMillis();
        if (now - start >= 5000)
        {
            game = false;
            Intent about = new Intent(this, SplashScreen.class);
            startActivity(about);
        }
    }
}

Solution

  • I believe you want to implement a Screen with few seconds delay and then start your main Application. Just like a Splash Screen Before the Main Application Starts Right?

    Then this will help you!

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /** set time to splash out */
        final int welcomeScreenDisplay = 4000;
        /** create a thread to show splash up to splash time */
        Thread welcomeThread = new Thread() {
    
        int wait = 0;
    
        @Override
        public void run() {
        try {
        super.run();
        /**
        * use while to get the splash time. Use sleep() to increase
        * the wait variable for every 100L.
        */
        while (wait < welcomeScreenDisplay) {
        sleep(100);
        wait += 100;
        }
        } catch (Exception e) {
        System.out.println("EXc=" + e);
        } finally {
        /**
        * Called after splash times up. Do some action after splash
        * times up. Here we moved to another main activity class
        */
        startActivity(new Intent(CurrentActivity.this, NextActivity.class));
        finish();
        }
        }
        };
        welcomeThread.start();
    }
    

    This is a Screen for 4 seconds of Delay.