代码之家  ›  专栏  ›  技术社区  ›  just_another_coder

活动完成方法是否等待完成?

  •  3
  • just_another_coder  · 技术社区  · 16 年前

    使用此示例代码,活动被告知完成,但随后继续执行并显示消息。我在想为什么会这样。我能想到的唯一解决办法是在完成后放置“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();
    }
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   Samuh    16 年前

    你说得对。调用finish()不会立即完成活动。代码路径中所有可到达的语句都将在活动完成之前执行。取消对返回的注释时,Toast不是执行块的一部分。