使用此示例代码,将告诉活动完成,但随后将继续执行并显示消息。我想知道为什么会这样。我能想到的唯一解决办法是在完成后放置“return”。
public void someMethod() {
if( valueIsTrue) {
startActivity(new Intent(this, NewActivity.class));
CurrentActitivy.this.finish();
// return; // if uncommented, Toast doesn't show
}
Toast.maketext(this, "Some message", Toast.LENGTH_SHORT).show();
}
最佳答案
你是对的。调用finish()不会立即完成活动。代码路径中的所有可访问语句都将在活动完成之前执行。当您取消对返回的注释时,toast不是执行块的一部分。