tags:

views:

54

answers:

1

Hi there

In my android game I want to display a screen when it loads up with my logo saying I built the game, then after a few seconds I want this to switch to the menu screen, and then after a game is started, the screen changes to the game screen for the action to start.

I looked in to ViewFlipper but I have since concluded this is not right for the job. My question therefore is how does one go about controlling what screen/View is displayed programatically? Also, at the end of a level I want the screen to switch to a 'Stage Cleared' view before loading back in to the action.

Many thanks

A: 

Use a different Activity for each screen you want to display. For example, use an Activity to display your splash screen and once you're done showing that, start your main menu Activity from within your splash screen Activity.


Edit:

public class SplashActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_activity);
    }

    public void onResume() {
        super.onResume();
        self = this; // We need a reference to this Activity below.
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(5000); // Sleep for 5 seconds
                    Intent i = new Intent(self, MainMenuActivity.class);
                    self.startActivity(i);
                    self.finish();
                } catch (InterruptedException e) {
                    // Catch exception
                }
            }
        }.start();
    }
}

This starts SplashActivity for 5 seconds and then starts MainMenuActivity and finishes itself. The splash screen is initialized through setContentView(R.layout.splash_activity);; the layout should dictate how your logo is drawn. This also means the splash screen doesn't animate.

Andy Zhang
Ok so if I set the main launcher to be the splash screen, then within it how do I attach the drawable and display it for like 5 seconds. I imagine this will involve sleep(ms), but don't know how to set the picture to appear.Also once this is done would I use start activity in the 'fire and forget' fashion because I am done with the splash screen activity after this point?Many thanks again
Greenhouse Gases
That's the basic idea. I'll post some code in my answer to specify in a couple of minutes.
Andy Zhang
Do you need an animated splash screen? That'd involve a lot more code, which would be a separate question about animations.
Andy Zhang
Thats a very detailed and helpful answer, many thanks. No I don't think it needs to be animated. I suppose if it comes to that I'll use a canvas and a draw method but I doubt it will.It's a game you see, so if multiple threads prove too expensive I'll hopefully be able to stop the old thread from the new activity that is called by the old thread.
Greenhouse Gases
It is probably smarter to use Handler and send a delayed Runnable. Both because calling context methods from another thread is _bad_ and because using Handler won't require an extra Thread.
alexanderblom
Where do I define 'self'? And is it of type SplashActivity? Thanks
Greenhouse Gases
Just noticed your comment above alexanderblom. I've used Handlers elsewhere in my program but how would I use this to send a delayed runnable?
Greenhouse Gases
@Greenhouse Oops, yes 'self' is type SplashActivity. I was editing this directly in my browser, my bad! :)You can send a delayed response by getting a handler (maybe from the ImageView holding your splash image) and calling postDelayed() with your Runnable that starts MainMenuActivity and '5000' as arguments.@alexanderblom Nice find! I wasn't aware there was a postDelayed(). Thanks!
Andy Zhang