26

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

Thanks in advance.

Ishtiaq
  • 71
  • 1
  • 1
  • 12
Nireesha
  • 261
  • 1
  • 3
  • 3

4 Answers4

19

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)

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • 3
    I believe this answer may be somewhat outdated. I ran into a problem using `finishActivity(int)` to return to an activity started with `startActivityForResult(int)`, and found it did not work *at all*. It took a rebuild, a reset, and finally implementing `finish()` ***instead*** before it actually worked. – Gnemlock Aug 22 '16 at 06:14
15

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 4
    Your answer is helpful, but you can make it better by including a summary or relevant portions of the pages you're linking to. This will also help your answer remain great even if the links you included break in the future. http://meta.stackexchange.com/questions/92505/should-i-flag-answers-which-contain-only-a-link-as-not-an-answer – Janusz Aug 19 '11 at 06:28
  • I have quoted required lines before your comments. You are taking time to refresh the page. – Naveed Aug 19 '11 at 06:29
3

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.

Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
Mohit Verma
  • 3,025
  • 1
  • 20
  • 36
1

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.

Vinay
  • 6,204
  • 6
  • 38
  • 55