libgdx/Android: graphics disappearing after the app is destroyed/paused

蓝咒 提交于 2019-12-11 03:57:15

问题


I was doing some tests and I realized that on my Nexus 5, when I hit the back key (or the home) -that is, when there's a change of context- and I go back to my game, the openGL context is lost.

There are no textures anymore (they show as black) or skins for the UI (the buttons are white).

I thought it was automanaged by libgdx automatically, right? So why is this happening?

The way I'm creating the textures is via TextureAtlas, like

TextureAtlas atlas;
TextureRegion bg;
atlas = new TextureAtlas(Gdx.files.internal("mainMenu.atlas"));
bg = atlas.findRegion("bg");

And then it's used with the batch.draw(bg, x, y, w, h);

I also tried creating the TextureRegion loading directly a Texture instead of a TextureAtlas (just in case but it should be the same) and I get the same result...

Anyone?

Edit: more specific code:

Screen class basics:

public class MainMenuScreen extends ScreenManager.Screen {

        private Game game;
    private InputMultiplexer inputMultiplexer = new InputMultiplexer();

    private MainMenuUi screenUi;
    private MainMenuView screenView;

    private TextureAtlas atlas;

    public MainMenuScreen(ConbiniGame game) {
        this.game = game;

        atlas = new TextureAtlas(Gdx.files.internal("mainMenu.atlas"));
        screenUi = new MainMenuUi(game);
        screenView = new MainMenuView(atlas);

        inputMultiplexer.addProcessor(screenUi.getInputProcessor());
        inputMultiplexer.addProcessor(screenView.getInputProcessor());

        Gdx.input.setInputProcessor(inputMultiplexer);
    }

    // ...
}

MainMenuView class where the TextureAtlas is being used...

public class MainMenuView {

    private Stage stage;
    private OrthographicCamera camera;
    private Viewport viewport;

    private TextureAtlas atlas;
    TextureRegion bg;

    public MainMenuView(TextureAtlas atlas) {
        atlas = atlas;
        bg = atlas.findRegion("bg");

        camera = new OrthographicCamera();
        camera.setToOrtho(false);
        viewport = new FitViewport(1080, 1920, camera);
        stage = new Stage(viewport);
    }

    public void update(float delta) {
        stage.act(delta);
    }

    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.getBatch().begin();
        stage.getBatch().draw(bg, 0, 0, stage.getCamera().viewportWidth, stage.getCamera().viewportHeight);
        stage.getBatch().end();

        stage.draw();
    }

    public InputProcessor getInputProcessor() {
        return stage;
    }
}

The code is just to show the use of the texture, with other parts removed


回答1:


You didn't provide enough information, but your problem is likely caused by using static in your code. Don't do that.

When you press the back button your app is closed. When you press the home button then your app is paused. Note that this are two different things. Therefor you might experience the problem not always when using the home button. This because while your app is paused, that android might decide to close it (to free memory), but it is not guaranteed to do so.

Either way, this is not related to the opengl context being lost. It is just closed. If the context would be really lost then libgdx (and later versions of android) will recover it for you.

When you close your app and then immediately start it again then Android might reuse the same VM for that instance of your app. This also means that any static variables will have the value they had from the previous run of your app. If any of those variables include (may be indirectly) any resources, then those resources won't be valid anymore.

tl;dr never use static in android applications.

The most common mistake (without seeing your code this is just guessing) is to access assets by using a singleton. E.g. MyGame.getInstance().assets.get("atlas",...); don't do that, it will (because of above reason) fail. Instead pass a reference to your MyGame instance to whichever classes needs it (e.g. your Screen: new MenuScreen(this);).



来源:https://stackoverflow.com/questions/31137930/libgdx-android-graphics-disappearing-after-the-app-is-destroyed-paused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!