The Life Cycle - s76/libgdx GitHub Wiki
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.
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:
public class MyGame implements ApplicationListener {
public void create () {
}
public void render () {
}
public void resize (int width, int height) {
}
public void pause () {
}
public void resume () {
}
public void dispose () {
}
}
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 signature | Description |
---|---|
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 just before dispose() when exiting the application.A good place to save the game state. |
resume () |
This method is only called on Android, when the application resumes from a paused state. |
dispose () |
Called when the application is destroyed. It is preceded by a call to pause() . |
The following diagram illustrates the life-cycle visually:
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.