Reputation: 709
I have multiple devices that run Android OSes. I've been building a LibGDX game, and have been having a problem with it where, every time the user pressed the home button and then proceeds to reenter the app, the app restarts. I don't want this. I've been talking to a lot of people lately trying to figure out why my resume()
method doesn't run on my devices. This is the method which would help me reload my data/assets and keep the game it was before the user exited.
So tell me: Why do my devices not run this pause() method, but a lot of other people's devices DO?
For the record: My two devices that I test on are a Galaxy S4 phone and a Nexus 7 tablet.
Upvotes: 3
Views: 1130
Reputation: 19776
To complete P.T.'s answer I would like to add the following trick. It is really not intuitive that LibGDX doesn't call resume in the first start. But you can easily simulate that yourself.
public void create() {
resume();
}
public void resume() {
// this is the only place where you will initialize all assets and your screen/game
}
Don't do anything in create()
and just forward the work to resume()
. That way you won't have any code duplication and your initcode will always run on.
Upvotes: 2
Reputation: 25177
You do not have any control over how the Android system manages the lifetime of your application when its not in the foreground. As such you need to deal with all the possible transitions. The device is free to terminate your app when it is backgrounded (especially if its using a relatively large amount of resources).
In Libgdx the resume
method is only invoked on an actual resume from suspend (i.e., long-press home to switch to a different app and then long-press home to switch back). Other frameworks (including bare Android) often invoke resume
on first-start, so that may be a source of confusion. (Just put the first-start work in your create
callback.)
For some more details on the lifecycle of Libgdx Android apps and how to recreate the various cases, check out: http://bitiotic.com/blog/2013/05/23/libgdx-and-android-application-lifecycle/
Upvotes: 3