Difference between finish() and System.exit(0)
Asked Answered
V

4

46

I'm talking about programming in android.

In early days I thought that, finish() closes current activity and go back to the previous in Activity stack, and System.exit(0) closes the whole application.

But I was wrong. I made a small experiment and understood that Both will finish only the current Activity.


The only differences that I could notice is that, in Android 2.3.3

  • The ActivityResult is propagated back to onActivityResult() using finish(). Whereas onActivityResult() not called for System.exit(0).

But in Android 4.2.2, onActivityResult() is called for both! and Intent was null for exit(). (I tested only in these 2 devices)

  • There is a time lag when using exit() whereas finish() is faster.(seems like more background operations are there in exit())

So,

  1. what's the difference between two?

  2. In which situations, I can use exit()?

I believe there is something more that I'm missing in between the two methods. Hope somebody can Explain more and correct me.

Thanks

EDIT UPON REQUEST:

Make an Android application with 2 Activities. Call second Activity from Launcher activity using Intent. Now, inside the second activity, upon a button click, call System.exit(0);. "The VM stops further execution and program will exit."????(according to documentation)

I see first activity there. Why? (You are welcome to prove that I'm wrong/ I was right)

Vault answered 17/8, 2013 at 18:26 Comment(5)
#2034414. check the answers hereAleurone
@Raghunandan: That's a good one. Everyone please have a look. But a vast amount of info. Can anyone tell in brief to fit my subject?Vault
never use System.exit() i would not recommend it. Use finish(). For navigation you can use navigation drawer.Aleurone
This is interesting. Why didn't the application close as documentation states?Harless
Excellent article about why System.exit(0) should not be used: A cautionary tale on Android: do not call System.exit().Henslowe
M
37

Actually there is no difference if you have only one activity. However, if you have several activities on the stack, then:

  • finish() - finishes the activity where it is called from and you see the previous activity.
  • System.exit(0) - restarts the app with one fewer activity on the stack. So, if you called ActivityB from ActivityA, and System.exit(0) is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityA
Mince answered 13/5, 2015 at 14:27 Comment(1)
There's actually a big difference even if you have only one Activity. System.exit() shuts down the VM, so everything stored in static variables, singletons, and heap memory reserved by JNI code will be cleared. This doesn't happen when finishing the only Activity in an application because the VM and your Application instance are still alive.Darbydarce
G
14

According to android Developer -

finish()

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

System.exit(0)

The VM stops further execution and program will exit.

Geniculate answered 17/8, 2013 at 18:31 Comment(10)
System.exit() does not kill your app if you have more than one activity on the stack. Please check it.Vault
@Nizam: "System.exit() does not kill your app if you have more than one activity on the stack" -- you are welcome to provide proof of your claim that the process is not terminated, by uploading an app that demonstrates this behavior, along with the steps you took to confirm that the process was not terminated.Frightfully
@Frightfully I didn't said that the process is not terminated. And I don't know what is happening inside. In abstraction, whether I'm using finish() or exit() inside an activity, the previous activity is shown. that's what I meant by app not killed.Vault
@Nizam: "that's what I meant by app not killed" -- then you are welcome to provide evidence that the documentation ever claimed that System.exit() would cause an app to be "killed", for whatever definition you have of "killed".Frightfully
@CommonsWare: Please correct me if I'm wrong. See the answer above. I used "killed" instead of "exit" there.Vault
@Frightfully Isn't it very un-recommended to use System.exit() on Android anyway? What happens if it has a foreground service, for example? About the process killing, as I've noticed it also gets restarted if called on a task that has 2 Activities.Sabulous
@androiddeveloper: "Isn't it very un-recommended to use System.exit() on Android anyway? " -- yes.Frightfully
@Frightfully Very odd that the docs about it doesn't say it. I think it could be deprecated, even.Sabulous
@Frightfully if it is unrecommended, then what is the recommended way to restart your app? For example, in case when a user logs out, all the static variables have to be destroyed, shared preferences must be re-fetched etc.Eichman
@SouravKannanthaB: If you are using a dependency inversion framework (Dagger+Hilt, Koin, etc.), they usually have a facility to reset your managed singletons.Frightfully
W
9

According to the documentation, The program will exit.
But it seems a bug in the documentation. In case of a java program, it is correct. But coming to Android, You will see the previous Activity from the stack.

Since Android coding is done using java coding, most of the documentation is same as those for java.
From documentation,

System.exit(0)
The VM stops further execution and program will exit.

For Android aspect, we have to replace the word 'program' with something else. May be Activity or Context.

Weakminded answered 28/12, 2013 at 13:6 Comment(0)
A
3

Sa Qada answer is correct after my testing.

finish will close this activity and back to prevous.

but exit will close current activity too and empty all the activity in freeze and start again the previous activity

Actually there is no difference if you have only one activity. However, if you have several activities on the stack, then:

finish() - finishes the activity where it is called from and you see the previous activity. System.exit(0) - restarts the app with one fewer activity on the stack. So, if you called ActivityB from ActivityA, and System.exit(0) is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityA

Ascensive answered 10/5, 2016 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.