Android OpenGL ES:恢复/暂停活动

时间:2012-12-30 18:23:25

标签: android opengl-es android-activity lifecycle

我很难理解在Activity,GLSurfaceView和Renderer之间拆分初始化和维护应用程序状态的正确方法。 假设我有一个复杂对象,复杂状态的游戏,并希望保留状态并在每个onResume上加载它。 我的初始化是这样的:

public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    gamescreenView = (GLGameScreenSurface) findViewById(R.id.GLGamescreenView);
    ...
    if (savedInstanceState == null) { 
        Game.init();
        resuming = false;
    } else {
        gamedata = (GameData) savedInstanceState.getSerializable("gamedata");
        resuming = true;
    }
    renderer = new MyRenderer(this, gamedata, gamescreenView,
            handle, resuming);
    gamescreenView.setRenderer(renderer);
}
@Override
public void onPause(){
    super.onPause();
    gamescreenView.onPause();
    gamedata = renderer.gamedata;
}
@Override
public void onResume(){
    super.onResume();
    gamescreenView.onResume();
    renderer.gamedata= gamedata;
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putSerializable("gamedata", renderer.gamedata);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  gamedata = (UniverseHolder) savedInstanceState.getSerializable("gamedata");
}

这是我的渲染器:

public class MyRenderer implements GLSurfaceView.Renderer {
public WideOpenRenderer(Context context, UniverseHolder universe, GLGameScreenSurface gamescreenView, Handler handler, boolean resuming) {
    this.context = context;
    this.handler = handler;
    parent = gamescreenView;
    this.gamedata = gamedata;
    this.resuming = resuming;
}
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
            GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Log.d("mydebug", "" + resuming);
            gamedata.init();
}
...

但是这种方法不起作用,根据日志,恢复总是假的。

0 个答案:

没有答案