Activity finish method waits to finish?
With this sample code, the activity is told to finish, but continues execution afterwards and displays the message. I'm trying to understand why this happens. The only fix I can think of is to place 'return' after finish.
public void someMethod() {
if( valueIsTrue) {
startActivity(new Intent(this, NewActivity.class));
Cur开发者_开发知识库rentActitivy.this.finish();
// return; // if uncommented, Toast doesn't show
}
Toast.maketext(this, "Some message", Toast.LENGTH_SHORT).show();
}
You are correct. Calling finish() does not finish the activity immediately. All the reachable statements in the code path will execute before the activity is finished. When you uncomment the return the Toast is not part of the execution block.
精彩评论