A libGDX application has a well defined life-cycle, governing the states of an application, like creating, pausing and resuming, rendering and disposing the application.

ApplicationListener

An application developer hooks into these life-cycle events by implementing the ApplicationListener interface and passing an instance of that implementation to the Application implementation of a specific back-end (see The Application Framework). From there on, the Application will call the ApplicationListener every time an application level event occurs. A bare-bones ApplicationListener implementation may look like this:

  1. public class MyGame implements ApplicationListener {
  2. public void create () {
  3. }
  4. public void render () {
  5. }
  6. public void resize (int width, int height) {
  7. }
  8. public void pause () {
  9. }
  10. public void resume () {
  11. }
  12. public void dispose () {
  13. }
  14. }

One can also derive from the ApplicationAdapter class if not all interface methods are of relevance.

Once passed to the Application, the ApplicationListener methods will be called as follows:

Method signatureDescription
create ()Method called once when the application is created.
resize (int width, int height)This method is called every time the game screen is re-sized and the game is not in the paused state. It is also called once just after the create() method.
The parameters are the new width and height the screen has been resized to in pixels.
render ()Method called by the game loop from the application every time rendering should be performed. Game logic updates are usually also performed in this method.
pause ()On Android this method is called when the Home button is pressed or an incoming call is received. On desktop this is called when the window is minimized and just before dispose() when exiting the application.
A good place to save the game state.
resume ()This method is called on Android, when the application resumes from a paused state, and on desktop when unminimized.
dispose ()Called when the application is destroyed. It is preceded by a call to pause().

The following diagram illustrates the life-cycle visually:

The Life-Cycle - 图1

Where is the main loop?

libGDX is event driven by nature, mostly due to the way Android and Javascript work. An explicit main loop does not exist, however, the ApplicationListener.render() method can be regarded as the body of such a main loop.

See also

libGDX and Android lifecycle if you are aiming for Android. The article also explains why you should not use static variables.

Prev | Next