2

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

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);
}
}
4

1 回答 1

2

像这样做:

@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");
}

干杯。

于 2013-08-23T03:46:35.983 回答