Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an activity after a certain amount of time?

I would like to implement a SplashScreen in my app. I found the best and easiest way is to launch an activity that shows a layout with an image view at the launch of the app and then adding android:noHistory="true" attribute to the manifest. Now, how do I set the splashscreen activity to launch the MainActivity class after a certain amount of time? Lets say 2 seconds?

This is my splashscreen activity

public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);
}
}
like image 344
borislemke Avatar asked Feb 22 '23 22:02

borislemke


2 Answers

use

handler.postDelayed(runnable, delayinmilliseconds(2000 in your case));

final Runnable runnable = new Runnable()
{
    public void run() 
    {
       //start the new activity here.
    }
};
like image 74
Yashwanth Kumar Avatar answered Mar 04 '23 01:03

Yashwanth Kumar


Here is a complete sample.

package com.test.splash;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

public class splash extends Activity {

    private static final int STOPSPLASH = 0;
    //time in milliseconds
    private static final long SPLASHTIME = 3000;a

    private ImageView splash;

    //handler for splash screen
    private Handler splashHandler = new Handler() {
        /* (non-Javadoc)
         * @see android.os.Handler#handleMessage(android.os.Message)
         */
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case STOPSPLASH:
                //remove SplashScreen from view
                splash.setVisibility(View.GONE);
                break;
            }
            super.handleMessage(msg);
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
            splash = (ImageView) findViewById(R.id.splashscreen);
            Message msg = new Message();
            msg.what = STOPSPLASH;
            splashHandler.sendMessageDelayed(msg, SPLASHTIME);
    }
}
like image 44
san Avatar answered Mar 04 '23 02:03

san