Nireesha
Nireesha

Reputation: 271

Whats the difference between finish() and finishActivity(int requestCode) in android

Can anyone explain me the difference between finish() and finishActivity(int requestCode). And the situation of where to use them aptly.

Thanks in advance.

Upvotes: 27

Views: 49026

Answers (4)

Naveed
Naveed

Reputation: 42093

Read Following:

public void finish ()

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

public void finishActivity (int requestCode)

Force finish another activity that you had previously started with startActivityForResult(Intent, int).

For further reading have a look at the documentation.

Upvotes: 16

Vineet Shukla
Vineet Shukla

Reputation: 24031

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

finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int)

Upvotes: 22

Mohit Verma
Mohit Verma

Reputation: 3025

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

finishActivity(int requestCode) Force finish another activity that you had previously started with startActivityForResult(Intent, int).

requestCode The request code of the activity that you had given to startActivityForResult(). If there are multiple activities started with this request code, they will all be finished.

Upvotes: 3

Vinay
Vinay

Reputation: 6322

So basically you can call other Activities in Android from another Activity via an Intent in Android. When you call startActivityForResult, you're calling another Activity in hopes that a result of code/change in the state of your app will happen. For example, I run my Main Activity, however I call another Activity that sets various fields/variables in the app to certain values (i.e. a user setting up the app's settings). Then, when that Activity has finished and you must return to the Activity that invoked it, you may call finishActivity to send a requestCode that will flag whether the Activity invoked has performed in a way that you desired.

Upvotes: 1

Related Questions