Libgdx's Application Life-Cycle and Interface
The Application Life-Cycle in Libgdx is a well-defined set of distinct system states. The list of these states is pretty short: create, resize, render, pause, resume, and dispose.
Libgdx defines an ApplicationListener
interface that contains six methods, one for each system state. The following code listing is a copy that is directly taken from Libgdx's sources. For the sake of readability, all comments have been stripped.
public interface ApplicationListener { public void create (); public void resize (int width, int height); public void render (); public void pause (); public void resume (); public void dispose (); }
All you need to do is implement these methods in your main class of the shared game code project. Libgdx will then call each of these methods at the right time.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The following diagram visualizes the Libgdx's Application Life-Cycle:
Note that a full and dotted line basically has the same meaning in the preceding figure. They both connect two consecutive states and have a direction of flow indicated by a little arrowhead on one end of the line. A dotted line additionally denotes a system event.
When an application starts, it will always begin with create()
. This is where the initialization of the application should happen, such as loading assets into memory and creating an initial state of the game world. Subsequently, the next state that follows is resize()
. This is the first opportunity for an application to adjust itself to the available display size (width and height) given in pixels.
Next, Libgdx will handle system events. If no event has occurred in the meanwhile, it is assumed that the application is (still) running. The next state would be render()
. This is where a game application will mainly do two things:
- Update the game world model
- Draw the scene on the screen using the updated game world model
Afterwards, a decision is made upon which the platform type is detected by Libgdx. On a desktop or in a web browser, the displaying application window can be resized virtually at any time. Libgdx compares the last and current sizes on every cycle so that resize()
is only called if the display size has changed. This makes sure that the running application is able to accommodate a changed display size.
Now the cycle starts over by handling (new) system events once again.
Another system event that can occur during runtime is the exit
event. When it occurs, Libgdx will first change to the pause()
state, which is a very good place to save any data that would be lost otherwise after the application has terminated. Subsequently, Libgdx changes to the dispose()
state where an application should do its final clean-up to free all the resources that it is still using.
This is also almost true for Android, except that pause()
is an intermediate state that is not directly followed by a dispose()
state at first. Be aware that this event may occur anytime during application runtime while the user has pressed the Home button or if there is an incoming phone call in the meanwhile. In fact, as long as the Android operating system does not need the occupied memory of the paused application, its state will not be changed to dispose()
. Moreover, it is possible that a paused application might receive a resume
system event, which in this case would change its state to resume()
, and it would eventually arrive at the system event handler again.