user1579339
user1579339

Reputation:

Re-load the app after sleep

I found this thread on the libgdx forum and i have the same problem…

I used libGDx and I made a game in August 2012. In this game when I press the stand-by button of my phone(and the screen turns off) and then I press it again, the screen is the same that it was before press the stand by button. In the code I didn't write nothing about it, now I want to make another game(using the assetsManager in the splash screen to upload the resources) and when I press the stand by button of my phone and then I press it again the game restarts! so i see the splash-screen again

I used something like this:

@Override
public void resume()
{
    super.resume();
    this.setScreen(mainMenuScreen);
}

But does not work..

Any solutions? Thanks a lot!!

PS: this is the entire Game class..

public static Screen gameScreen;
public static Screen mainMenuScreen;
public static Screen chooseTimeScreen;
public static Screen creditsScreen;
public static AssetManager manager = new AssetManager();;
public static SpriteBatch batcher;
boolean create = false;


@Override
public void create()
{

    Gdx.app.log("----------------", manager + "");

    if (manager == null)
    {
        manager = new AssetManager();
        batcher = new SpriteBatch();
        setScreen(new SplashScreen(this, manager));
    }
    else
    {
        batcher = new SpriteBatch();
        setScreen(mainMenuScreen);
    }

}


@Override
public void dispose()
{
    super.dispose();
    manager.dispose();
    batcher.dispose();

    if(gameScreen != null) gameScreen.dispose();
    if(mainMenuScreen != null) mainMenuScreen.dispose();
    if(chooseTimeScreen != null) chooseTimeScreen.dispose();
    if(creditsScreen != null) creditsScreen.dispose();
}

Upvotes: 1

Views: 4002

Answers (3)

lefay1982
lefay1982

Reputation: 11

A Solution is

  1. project.properties file modify

    target=android-14

  2. AndroidManifest.xml file modify

    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

    add screenSize in the android:configChanges

Upvotes: 1

P.T.
P.T.

Reputation: 25177

You are using static variables to track state that needs to get re-loaded on application resume. Because of the way the Android lifecycle works, a "static" variable reference will (often, but not always) stay valid across a resume, but the actual Android "Activity" will be destroyed. You should re-create all the resources on resume. (In your case I think you're not creating them, but you should, and then you use them and they refer to stale state from the old, now dead Activity.)

See Android static object lifecycle for a description.

The short answer is that, with libGDX, unless you're absolutely sure about what you are doing, do not use static variables to track application state, always (re-)create your state in the create method.

Upvotes: 0

IAmGroot
IAmGroot

Reputation: 13855

check also what you are doing in the onCreate method. This is sometimes called when the screen is awoken from sleep. If you are re-creating your splash screen, or resetting it here, it will be an issue.

Also, your code states resume(), should it not be onResume()

To avoid recreating your assetmanager in onCreate Do something like:

if(manager == null)
    manager = new AssetManager();

Upvotes: 0

Related Questions