Civitas
Civitas

Reputation: 75

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()

Upvotes: 1

Views: 1486

Answers (3)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

 explicitly finish the current activity or does android take care this ?

It depends on your requirement if you wants activity A while coming back form activity B still there so you need not to call finish but if you does not want activity A when coming back form activity B then you should call finish ....

Upvotes: 1

AAnkit
AAnkit

Reputation: 27549

No it is not compulsory.

finish()

finish method state that "Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult()."

reference link >> link

Upvotes: 2

FoamyGuy
FoamyGuy

Reputation: 46846

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

Upvotes: 8

Related Questions