0

I'm not very sure how to preserve all the variables initialiazed on an OpenGL ES app for Android. My app is quite slow to initialize, and if I don't preserve the initialiazed variables, every time I turn off and on again the screen, or press the home key and return to the app, all of the initial operations start again, and this causes a slow return to the game, and it goes back to the first state of the game. How can I preserve the app status to go back to it without having to initialize everything again if the app is put in background? Do I have to modify the onSaveInstanceState function?

Edit: What I want is that the app doesn't reload the context, and that returning to it doesn't last like if it were first initiated

WaLi
  • 93
  • 12
  • Please explain what you mean by _state_. The GL context? Some state that can be recreated out of assets? The temporary in-game state? – Stefan Hanke Apr 06 '12 at 11:12
  • I explained on the question. What I want is that it doesn't take forever just to return to an already initialized instance of the app, for example if I tun off and on the screen, like almost every android game do. – WaLi Apr 06 '12 at 11:51

2 Answers2

1

You need to use onSaveInstanceState and onRestoreInstanceState to save and load the variables,

I think Saving Android Activity state using Save Instance State is what you need

Community
  • 1
  • 1
  • that's for saving the "state", I mean, where the user were. But I want to not load the gontext every time the screen goes off – WaLi Apr 06 '12 at 13:28
1

I still do not really comprehend your question. Anyway: you will have no other choice, depending on what state is thrown away. As in my comment above, there are at least 3 types of state:

  1. (Game) state that can be recreated from assets.
  2. (Game) state that represents the current users view (see answer from @AndrewWilkinson)
  3. The OpenGL context.

Have a look at an Activity's lifecycle. When the process is not kicked out of memory, states 1. and 2. are preserved, and you need not worry. Bad thing is, you cannot know, so you need to prepare the app from being kicked out of memory and later recreated. So you need to preserve state 2. at anytime.

For state 3.: On each call to GLSurfaceView.Renderer.onSurfaceCreated, you can be sure there is no OpenGL context anymore. You need to upload everything that is needed for displaying again. Setting the preserve flag might help, but you cannot be sure.

If loading takes too long, consider to show a simple pause screen that does not take too long loading and keeps the user happy. Consider doing stuff in a more lazy fashion, s.t. some content is loaded on-demand and not eagerly.

Stefan Hanke
  • 3,458
  • 2
  • 30
  • 34