Skip to content

LibGDX Game and Screen Lifecycle

In the Saxion Game App, every game or screen goes through a lifecycle, which tells the program when to start, update, pause, or clean up resources. Understanding this lifecycle helps you write stable and efficient games.

Each screen that you build needs to implement the following methods: show(), hide() and render(float delta) method. Other methods from the lifecycle are by default hidden, but can be overriden.

Lifecycle

Figure 1. Lifecycle of each screen in LibGDX

show()

This method will be executed when the screen becomes active. You can initialize screen-specific resources in this method. This is where you load all your textures, fonts, colors, sounds and music for the specific screen.

Please note: the textures, fonts, sounds and music should be disposed in the hide() method, to free up the memory again and make room for the next screen that you want to show.

Example:

@Override
public void show() {
    GameApp.addTexture("player", "textures/player.png");
    GameApp.addColor("skyBlue", 135, 206, 235);
}

hide()

When switching to another screen, this method is called. Use this method to dispose screen-specific resources (textures, fonts, sounds and music). Colors don't need to be disposed.

Example:

@Override
public void hide() {
    GameApp.disposeTexture("player");
    GameApp.disposeFont("arial-32pt-styled");
    GameApp.disposeSound("gunshot");
    GameApp.disposeMusic("backgroundmusic");
}

render(float delta)

This method is called continuously, many times per second (frame rate). It is often seen as the game loop of the application. See the Game loop article for more information.

Make sure the super.render(delta) call is executed as the first line of the method, otherwise the window and the world coordinates are not in sync.

Example:

@Override
public void render(float delta) {
    // IMPORTANT: THIS CODE NEEDS TO BE THERE
    super.render(delta);

    // Your code here
    ...
}

resize(int width, int height) (optional)

This method is executed when the window is resized. There is no need to override this method, but you choose to do so (for your own reasons), please don't forget to call the super method. Otherwise the world coordinates are not correctly translated.

Example:

@Override
public void resize(int width, int height) {
    // IMPORTANT: THIS CODE NEEDS TO BE THERE
    super.resize(width, height);

    // Your code here
    ...
}

pause() (optional)

Executed when the game loses focus (another application is in the foreground or on mobile, the application is pushed to the background). You can use this methods to save temporary data or pause animations.

resume() (optional)

Called when the game regains focus. You want to resume animations here and restart timers.

dispose() (optional)

Called when the application is being terminated. You can use this method to free memory by disposing textures, sounds, or other resources that are used in multiple screens (and have not been disposed in the hide() method already).

Notes

  • Initialize screen-specific resources in show().
  • Textures, sounds and other resources should be disposed in the hide() method.
  • Use delta in render(float delta) to make animations and movement smooth, even if the frame rate changes.
  • resize() is automatically called when the window size changes, useful for scaling the game world with ScalableGameScreen.