0

我不太确定如何保留在适用于 Android 的 OpenGL ES 应用程序上初始化的所有变量。我的应用程序初始化很慢,如果我不保留初始化的变量,每次我关闭并再次打开屏幕,或者按主页键并返回应用程序时,所有初始操作都会重新开始,并且这会导致游戏缓慢返回,并回到游戏的第一个状态。如果将应用程序置于后台,如何保留应用程序状态以返回它而无需再次初始化所有内容?我必须修改onSaveInstanceState功能吗?

编辑:我想要的是应用程序不会重新加载上下文,并且返回它不会像第一次启动时那样持续

4

2 回答 2

1

您需要使用 onSaveInstanceState 和 onRestoreInstanceState 来保存和加载变量,

我认为使用 Save Instance State 保存 Android Activity 状态是您所需要的

于 2012-04-06T12:48:49.117 回答
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.

于 2012-04-07T08:15:24.377 回答