2

我很难理解在 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();
}
...

但它不是这样工作的,根据日志,恢复总是错误的。

4

0 回答 0