create(), show(), Game, ApplicationListener and Screen - It's better to ask late then to never ask

It's better to ask late than to never ask

The 22th of April 2014, a friend of mine posted this on Facebook:

Things i always wanted to ask Robin Stu but didnt.

1) Whats the difference between create() and show()

2) Why sometimes we extend Game and others, we implement Screen?Whats the difference

3) Which method is called first - Create or show

Those questions might appear in my presentation and i dont want look stupid.The most important thing is not NOT-Being-Stupid, but not look stupid. :D

Those are essential questions you don't want to be confused with. So it's good you asked, André.

ApplicationListener#create() and Screen#show()

The first thing you will notice is that they are methods of two different interfaces. Let's investigate the difference between ApplicationListener and Screen and you will see what the difference is.

99.9% of the time, you just have one ApplicationListener in your game. Screens however, you might have dozens. For an introduction, I'll just quote the documentation:

  • ApplicationListener:

    An ApplicationListener is called when the Application is created, resumed, rendering, paused or destroyed. [...]
    The ApplicationListener interface follows the standard Android activity life-cycle and is emulated on the desktop accordingly.

  • Screen:

    Represents one of many application screens, such as a main menu, a settings menu, the game screen and so on.

The ApplicationListener, as the name suggests, listens to the application life cycle. A Screen only listens to what a Game (implements the ApplicationListener) tells it. You probably remember this line from your desktop project's main class:

new LwjglApplication(new YourGame(), cfg);

What exactly happens here? YourGame implements the ApplicationListener.
"But mine just extends Game!" Well, Game implements the ApplicationListener. So let's reimagine the same line like this:

new LwjglApplication(new YourApplicationListener(), cfg);

The LwjglApplication is the application the (Your)ApplicationListener listens to. When it is created, the create() method is called. If your ApplicationListener is a Game, you usually set the initial Screen in that method using something like setScreen(new YourScreen());. The setScreen(Screen) method then calls the show() method of that Screen:

Sets the current screen. hide() is called on any old screen, and show() is called on the new screen, if any.

So to sum up:
1) and 2) create() is called when the game is started and show() is called when the Screen in question is set to be displayed.
3) create() is called first and you call show() via Game#setScreen() in it.

Hopefully this answered your questions, André.

Feedback and Knowledge Base