Skip to content

Screen Lifecycle

damios edited this page Sep 1, 2024 · 6 revisions

Managed Game

ManagedGame offers all the methods, that Game, ApplicationAdapter or ApplicationListener would, as well. Nearly all of those method calls are handed down to the screens via the screen manager, so it is important to not forget calling the super methods when overriding any of these!

ManagedScreen

Method Description
show() Called when this screen becomes the active screen. Note that at first, the screen may be rendered as part of a transition. If you want to reuse screen instances, this is the place where the screen should be reset. Right after this method, #resize(int, int) is called.
hide() Called when this screen is no longer the active screen for a ManagedGame and a possible transition has finished.
render() Called when the screen should render itself. Game logic updates are usually also performed in this method. Before this method is called, the previously rendered stuff is cleared with the #getClearColor().
If you are using any Viewports, be sure to apply them first! When using the same SpriteBatch as the transitions, don't forget to set the projection matrix before using it.

For example:
viewport.apply(); // you need to apply your viewport first 
spriteBatch.setProjectionMatrix(viewport.getCamera().combined);

// And then render your stuff:
spriteBatch.begin();
// ...
spriteBatch.end();
resize(int width, int height) This method is called when the game is resized while this screen is rendered and the new size is different to the previous one. In addition, this method is called once right after #show().
The parameters are the new width and height the screen has been resized to in pixels.
pause() Called when the application is paused while this screen is rendered. The game is usually paused when it is not active or visible on-screen. On Android, this is the case when the home button is pressed or an incoming call is received. On desktop, this method is called when the game is minimized. However, #pause() is not called, when the game just loses focus. This has to be detected with a Lwjgl3WindowListener. On iOS, this method is called when the app is about to move from the active to inactive state, e.g. when an incoming call is received. On web, pause events are tied to the hidden document property, which determines whether the page is not even partially visible.
resume() Called when the application is resumed from a paused state; usually when it regains focus. On (older) Android devices, the OpenGL context might be lost on pause. In general, libGDX will re-create OpenGL objects that were lost, but if, for example, there are any run-time created textures, they will have to re-created in this method.
dispose() Is called automatically in two cases:
  • when the screen manager is disposed and this screen was pushed, but not yet hidden (#hide()); it does not matter whether the screen was actually rendered. In other words, #dispose() is called for the current screen, a screen which is rendered as part of a transition, as well as any screens still queued to be shown.
  • If users want automatic disposing for screens on which #hide() has been called previously (and which were not pushed a second time), this can be enabled via ScreenManager#setAutoDispose(boolean, boolean).