3

I would like to display a splash screen while everything is initializing in the onCreate() method, yet components that I need to draw things to the screen are also initializing, therefore there's a black screen when I start the app and after the onCreate() method has completed then only is the first screen drawn. Instead of having the black screen I'd like a splash screen.

Here's my code in the onCreate method:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Acquire a wakeLock to prevent the phone from sleeping
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");

        // Setup all the Game Engine components 
        gameEngineLog = new WSLog("WSGameEngine");
        gameEngineLog.setLogType(this.gameEngineLogType);
        gameLog = new WSLog(this.gameLogTAG);
        gameLog.setLogType(this.gameLogType);
        io = new FileIO(this, getAssets());
        audio = new Audio(this);
        wsScreen = new WSScreen(this, this.screenResizeType, this.customTopYGap, this.customLeftXGap, this.gameScreenWidth, this.gameScreenHeight);
        graphics = new Graphics(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended());
        renderView = new RenderView(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended(), FPS, maxFrameskippes);
        input = new Input(this, renderView, logGameEngineInputLog);
        setContentView(renderView);

        if(useOfAnalytics == true) {
            getGameEngineLog().w(classTAG, "Analytics has been enabled");
            analytics = new Analytics(this);
        }

        // Check that the developer has initialized the assets
        if(this.assets == null) {
            this.gameEngineLog.w(classTAG, "The assets for the game haven't been defined!");
        }

    }

How should I implement a splash screen, to avoid the black screen at the beginning?

Luke Taylor
  • 9,481
  • 13
  • 41
  • 73

2 Answers2

3

//avoid time consuming work in UI thread but if that is related to UI itself then do as below

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


            setContentView(R.layout.first_splash_lauoy);

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

                public void run() {
                  //Your time consuming work 
                  //with spiiner(if needed) & setContentView(<finalView>)
                  }, 1000);
        }

    }
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
3

Create a XML layout for this splash screen and set it as content view right after your super.onCreate():

super.onCreate(savedInstanceState);
setContentView (R.layout.splash_screen);

That should be enough. It would display this splash screen until your setContentView(renderView) is called.

MRD
  • 7,146
  • 1
  • 21
  • 21
  • setContentView(R.layout.splashscreen); // Do work setContentView(view); The first setContent view has no effect. – Luke Taylor Jun 21 '12 at 15:38
  • What do you mean? aren't you able to see the splash screen? Maybe the load time is so small that is not perceptible. In that case you would need to use the postDelayed method as it is said in the other answer. – MRD Jun 21 '12 at 16:20
  • I got it to work, all settled. I believe setContentView only has an effect once the onCreate() method has completed to execute. – Luke Taylor Jun 21 '12 at 16:32