Question

There are a lot of articles about how to save a state of a game and they are pretty good. But I have one conceptual misunderstanding where should I save the state?

My libGDX game has number of screens and pair of them are MainMenuScreen and MainSceneScreen these are inherited from Screen class. MainMenuScreen is shown at start of the game the MainSceneScreen little later.

What is the problem? I navigated to MainSceneScreen, forced Android to stop the application (I change a language settings on the device to achieve this). After that I select the application again and I can see MainMenuScreen is shown. But I want MainSceneScreen to be shown.

I suppose I should override resume method. But which one? I have class PsGame that extends Game class of libgdx. I put breakpoints to its resume method and it turned out that method was not called. I investigated the problem and I've found this strange code in the onResume method of AndroidApplication class in libGDX:

    if (!firstResume)
        graphics.resume();
    else
        firstResume = false;

My debugger said firstResume was true and didn't go to graphics.resume() line.

  1. What did I do wrong?
  2. What methods should I override?
Was it helpful?

Solution

I think you've narrowed this down correctly, and you should be restoring your saved state during the resume method call on your Game subclass. This should be called if you switch to some other app for a bit, and then switch back to your app (without going through the steps that will cause the app to be terminated). For whatever reason, libgdx does not forward the resume call for the initial startup (this design is probably a good idea in the long run, but is a bit confusing when starting out). Its easy enough to invoke your restore code from the create method in your Game (which is only invoked at application startup) to get the first start of your game to have 'resume' behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top