所以,我目前正在尝试编辑android的api提供的Snake游戏。我试图让它在按下菜单->设置->恢复后恢复游戏。但是,在我按下设置后,它并没有一路恢复,而是退出并从那里恢复。我正在使用 Intents 来恢复程序。
public boolean onOptionsSelected(MenuItem menu){
switch(menu.getItemId()){
case R.id.settings:
Intent prefActivity = new Intent(this,MyPreferences.class);
startActivityForResult(prefActivity, KEY_RESUME_RESULT);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch(requestCode){
case KEY_RESUME_RESULT:
if(resultCode==RESULT_OK){
if(mSnakeView.getMode() == SnakeView.PAUSE)
this.mSnakeView.setMode(SnakeView.RUNNING);
}
}
}
这是在 MyPreferences.class
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.snake_preferences);
resume = (Preference)findPreference(getString(R.string.key_resume));
onPreferenceTreeClick(this.getPreferenceScreen(),resume);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferencescreen,Preference preference){
super.onPreferenceTreeClick(preferencescreen,preference);
Intent intent = new Intent();
if(preference == resume){
setResult(Activity.RESULT_OK,intent);
finish();
}
return true;
}