有人可以给我一个解决方案,并解释他/她如何提出该特定解决方案吗?
非常感谢

1)当GUI从纵向旋转到横向或横向旋转时,所有局部变量都消失了,在该类的内容上写上了To-Do标记,因此isGameFinished变量被旋转处理并在应用被重新收集时再次收集旋转的



public class StatteActivity extends Activity{

private boolean isGameFinished;
private Button buttonFinishGame;

protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state);

//Get previous state for isGameFinished if it exists
//TO-DO

//Find button
buttonFinishGame = (Button) findViewByID(R.id.buttonFinishGame);
buttonFinishGame.setOnClickListener(new OnClickListener(){
public void onClick(View v){
isGameFinished = true;
}
});

if(isGameFinished){
finish();
}
}

protected void onSaveInstanceState(Bundle outState){
//To-Do
super.onSaveInstanceState(outState);
}
}

最佳答案

像这样做:

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    // Saving variables
    savedInstanceState.putBoolean("isGameFinished", isGameFinished);

    // Call at the end
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
    // Call at the start
    super.onRestoreInstanceState(savedInstanceState);

    // Retrieve variables
    isGameFinished = savedInstanceState.getBoolean("isGameFinished");
}


干杯。

08-06 08:57