因此,我实现了两次双击以退出我的mainactivity,如果我转到设置活动或游戏活动,则退出正常。但是,如果我进入mainactivity-> gameactivity-> gameoveractivity,然后按回去,则可以很好地启动mainactivity,但是再按两次就可以返回gameoveractivity,但我不确定为什么从gameoveractivity启动mainactivity时会调用完成。
主屏幕活动:
// button listeners
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent gameActivityIntent = new Intent(MainScreenActivity.this, GameActivity.class);
startActivity(gameActivityIntent);
overridePendingTransition(R.anim.righttocenter, R.anim.centertoleft);
finish();
}
});
settingsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent settingsActivityIntent = new Intent(MainScreenActivity.this, SettingsActivity.class);
MainScreenActivity.this.startActivity(settingsActivityIntent);
overridePendingTransition(R.anim.righttocenter, R.anim.centertoleft);
}
});
游戏活动:
// handle hardware back button
@Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameActivity.this, MainScreenActivity.class);
startActivity(mainScreenActivityIntent);
overridePendingTransition(R.anim.lefttocenter, R.anim.centertoright);
finish();
}
处理游戏过度活动的逻辑代码:
if (weight.getBounds().intersect(player.getBounds())) {
timer.cancel();
gameTimer.cancel();
player.setTouched(false);
save(score, time);
Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class);
this.getContext().startActivity(gameOverIntent);
((Activity) this.getContext()).finish();
}
游戏过度活动:
// handle hardware back button
@Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class);
mainScreenActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
GameOverActivity.this.startActivity(mainScreenActivityIntent);
overridePendingTransition(R.anim.lefttocenter, R.anim.centertoright);
finish();
}
编辑:通过添加以下内容来解决此问题:
gameOverIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
在创建gameoveractivityintent之后,在游戏逻辑代码中输入。
最佳答案
通过添加以下内容来解决此问题:
gameOverIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
在创建gameoveractivityintent之后,在游戏逻辑代码中输入。