4

I have an issue with my SplashScreenActivity, when I start my application on my phone it shows a white screen for about 0,5 seconds. The MainActitivy extends FragmentActivity and in the AndroidManifest I declare the SplashScreenActivity as launcher and portrait mode as screenOrientation.

The code:

public class SplashScreenActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splashscreen);
    randomSplash();

    Thread splashscreen = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
                startActivity(mainScreen);
            } catch (InterruptedException e) {

            } finally {
                finish();
            }
        }
    };
    splashscreen.start();
}

private void randomSplash(){
    Random random = new Random();
    int i = random.nextInt(4);

    LinearLayout ln = (LinearLayout) findViewById(R.id.splashscreen);

    switch(i){
    case 1: 
        ln.setBackgroundResource(R.drawable.splash1);
        break;
    case 2: 
        ln.setBackgroundResource(R.drawable.splash2);
        break;
    case 3: 
        ln.setBackgroundResource(R.drawable.splash3);
        break;
    default: 
        ln.setBackgroundResource(R.drawable.splash0);
        break;
    }
}
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/splashscreen"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
</LinearLayout>
Ronald Meijboom
  • 1,534
  • 3
  • 17
  • 37

3 Answers3

1
 Thread splashscreen = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
                startActivity(mainScreen);
            } catch (InterruptedException e) {

            } finally {
                finish();
            }
        }
    };
    splashscreen.start();

this is your problem UI thread to sleep not a very good idea use handler instead and I think it may cause an exception too.

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

            public void run() {
                // TODO Auto-generated method stub
                startActivity(new Intent(Splash_Activity.this,Main_Activity.class));
                finish();
            }
        }, 2000);
    }
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
1

You need to run this two actions in an AsyncTask:

setContentView(R.layout.splashscreen);
randomSplash();

put the setContentView in the doInBackground-method and in the postExecute method you run randomSplash.

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
1

Change SplashActivity theme in the AndroidManifest.xml file to this.

android:theme="@android:style/Theme.Translucent.NoTitleBar"
Aarun
  • 564
  • 1
  • 6
  • 13