2

I'm trying to do a splash screen for my app. i used the postDelayed method. this is my code:

   public class SplashScreenActivity extends Activity {

    private static final int SPLASH_DURATION_MS = 1500;

    private static final String TAG = SplashScreenActivity.class.getSimpleName();

    private Handler mHandler = new Handler();

    public static final int sdkVersion = Build.VERSION.SDK_INT;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        Random r = new Random();
        int imageNumber = r.nextInt(2 - 0) + 0;

        ImageView splashScreenBackground = (ImageView)findViewById(R.id.splash_screen_image);
        switch (imageNumber){
            case 0:
                if(sdkVersion > 20)
                    splashScreenBackground.setBackground(getDrawable(R.drawable.splash_screen_back));
                else
                    splashScreenBackground.setBackgroundDrawable(getResources().getDrawable(R.drawable.splash_screen_back));
                return;
            case 1:
                if(sdkVersion > 20)
                    splashScreenBackground.setBackground(getDrawable(R.drawable.buffon));
                else
                    splashScreenBackground.setBackgroundDrawable(getResources().getDrawable(R.drawable.buffon));
                return;
        }

        mHandler.postDelayed(mEndSplash, SPLASH_DURATION_MS);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mEndSplash.run();
        return super.onTouchEvent(event);
    }

    private Runnable mEndSplash = new Runnable() {
        public void run() {
            if (!isFinishing()) {
                mHandler.removeCallbacks(this);

                Log.d(TAG, "Debugging before intent");
                startActivity(new Intent(
                        SplashScreenActivity.this, MainActivity.class
                ));
                Log.d(TAG, "Debugging after intent");
                finish();
            }
        };
    };

}

and this is the call in the onCreate method:

mHandler.postDelayed(mEndSplash, SPLASH_DURATION_MS);

the problem is that the activity doesn't chnage until i press on the screen.

Another thing, the class MainActivity is charged and logs are showed but i don't get it in screen !!! I want to know what's the problem. Thanks

3 Answers3

2

You are using return in your switch instead of break, therefore the code with the handler is never reached.

Sebastian
  • 1,076
  • 9
  • 24
0
ImageView splashScreenBackground = (ImageView)findViewById(R.id.splash_screen_image);

splashScreenBackground.setImageResource(R.drawable.splash_screen_back);

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

        @Override
        public void run() {
    startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));

    }
  }, 1500);
Kishan Soni
  • 816
  • 1
  • 6
  • 19
-1

Try following code in your Splash Activity.

First, Declare following

private Timer mTimer;// timer for running splash screen for 1500 millisecods(i.e. 1.5 seconds)

Then, in onCreate(), put following code

mTimer = new Timer();
    mTimer.schedule(new TimerTask() {
        @Override
        public void run() { 

            startActivity(new Intent(
                    SplashScreenActivity.this, MainActivity.class
             mTimer.cancel();
            SplashScreenActivity.this.finish();

        }
    }, 1500);
techroid
  • 477
  • 4
  • 11