tags:

views:

68

answers:

1

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));
        CurrentActitivy.this.finish();

        // return;  // if uncommented, Toast doesn't show
    }

    Toast.maketext(this, "Some message", Toast.LENGTH_SHORT).show();
}
A: 

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.

Samuh