Search code examples
androidandroid-activityactivity-finish

Do I need to explicitly finish an Activity in Android?


When I am starting a new activity do I need to explicitly finish the current activity or does android take care this ?

This is what I write in activity A to start activity B:

Intent intent = new Intent(this, BActivity.class);
startActivity(intent);

Should I end A by calling next line after above mentioned two lines ?

this.finish()

Solution

  • In General no you shouldn't.

    The difference will be if you call finish in Activity A, While the user is in Activity B if they press the back button they will go back to whatever they were doing before opening your application. If you instead do not call finish in Activity A they will go back to Activity A

    If you DO call finish:

    Activity A -> Activity B -> [user press back] -> Homescreen (or whatever activity is on the stack below activity A)
    

    if you DO NOT call finish:

    Activity A -> Activity B -> [user press back] -> Activity A