2

I just came across this code in

Intent wizard = new Intent();
wizard.setClass(this, A.class);
wizard.putExtra("Domain", A.getInstance().B);
startActivity(wizard);
finish();

Why should we call finish() method here? What's it purpose?

sofs1
  • 3,834
  • 11
  • 51
  • 89
  • 1
    you might want to finish the current activity ? – Blackbelt Jan 15 '17 at 10:09
  • So the finish() is used to finish the current activity (activity which sends the intent) and not the activity which receives the intent. Am I right? – sofs1 Jan 15 '17 at 10:12
  • Related threads - [How does Activity.finish() work in Android?](https://stackoverflow.com/q/2590947/465053) & [what exactly Activity.finish() method is doing?](https://stackoverflow.com/q/10847526/465053) – RBT Aug 23 '18 at 23:57

4 Answers4

5

finish() Call this when your activity is done and should be closed, when starting an activity calling finish() will close the current activity and it will not be available in the Activity Stack.

enter image description here

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • "The ActivityResult is propagated back to whoever launched you via onActivityResult()". Lets say there are 3 activities. A calls B. In B we call activity C, and then we use finish() in B. Now in this example the activityResult of whom is propagated back to whom? – sofs1 Jan 15 '17 at 11:29
  • I didn't get ActivityResult part, please elaborate ..!! – W4R10CK Jan 15 '17 at 11:31
  • Could you explain "The ActivityResult is propagated back to whoever launched you via onActivityResult()" with an example of 3 activities. Especially how the result of an activity is sent back when finish() is called. – sofs1 Jan 15 '17 at 11:39
  • Not sure with the docs of [google](https://developer.android.com/reference/android/app/Activity.html#finish()), but when u call `finish()` from B the activityresult from B will be back to you again. Not sure with the meaning. – W4R10CK Jan 15 '17 at 11:51
4
finish(); 

finish is used for closing current activity.It is like you are sending data to other activity/Opening new activity and closing your current activity.

santosh kumar
  • 2,952
  • 1
  • 15
  • 27
4

You are not imperative forced to do that... normally people do that if and only if the want to destroy the activity that is starting the intent, but that must no be your case...

in the life cycle of android you will see:

assuming you activity is visible, then calling finish() method will call

onPause(), then onStop() and finally the onDestroy()

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • And finish() method belongs to Activity.class and the above states are of Activity life cycle. – sofs1 Jan 15 '17 at 11:20
3

Let's understand it using few quotes

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


onDestroy() - The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space.

when you call finish() on an activity, the methods followed by onDestroy() is executed.. eg: onPause()> onStop() > and onDestroy()they can be differ from the place you call finish()!

onDestroy() is meant for final cleanup - freeing up resources that you can on your own,closing open connections,readers,writers,etc. If you don't override it, the system does what it has to.

It inform the system that you want to finish the selected Activity so it wil call onDestroy() after you finish the activity.(but this does not mean that onDestroy() only gets called by finish(),system can do that when it's running out of resources after the activity is sent to the back state)

Also there are more interesting questions like Activity.finish() called but activity stays loaded in memory that you might like

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • @user3705478 if you had 3 Activities A > B and then C and if you used only finish on B(when you call C) .. then if you press back from C it will direct to A whatever the finished Activities will be removed from the back stack – Charuක Jan 15 '17 at 12:41