睡眠後重新加載應用程序 (Re-load the app after sleep)


問題描述

睡眠後重新加載應用程序 (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();
}

‑‑‑‑‑

參考解法

方法 1:

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();

方法 2:

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

方法 3:

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.

(by user1579339IAmGrootlefay1982P.T.)

參考文件

  1. Re‑load the app after sleep (CC BY‑SA 3.0/4.0)

#libgdx #Android






相關問題

為什麼原生 libmpg123 在帶有 libgdx 的 android 上花費這麼長時間? (Why is native libmpg123 taking so long on android with libgdx?)

睡眠後重新加載應用程序 (Re-load the app after sleep)

獲取實際觸摸位置 LibGDX (Get actual touch position LibGDX)

LibGDX - 只有可拖動的運動 (LibGDX - only draggable movement)

無法解析符號“android” - 在使用 libgdx 在 Android 應用程序中實現 Google Analytics 時 (Cannot resolve symbol 'android' - While implementing Google Analytics in Android App with libgdx)

如何從矩形數組中刪除隨機矩形? (How to remove random rectangle from rectangle array?)

球在傾斜平面上滾動 java libgdx (Ball rolling on an incline plane java libgdx)

libgdx - Intellij 類未找到異常? (libgdx - Intellij class not found exception?)

GL_COLOR_BUFFER_BIT 再生哪個內存? (GL_COLOR_BUFFER_BIT regenerating which memory?)

libGDX - 更改屏幕後的黑色按鈕和文本 (libGDX - Black buttons and text after changing screen)

帶有 9patch 的 LibGdx 標籤背景 (LibGdx label background with 9patch)

如何從平鋪中刪除對象? (How do I remove an object from tiled?)







留言討論