Difference between finish() and System.exit(0)

44,363

Solution 1

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

Solution 2

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.

Solution 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

Share:
44,363
Nizam
Author by

Nizam

Mobile Technology Lead at Tangentia technologies, Thiruvananthapuram, Kerala. Concentrating on Android Application development. Also interested in Web Development. ;) My native place is Kodungallur, the cultural capital of Kerala. Also, the first Mosque in India resides here :) Happy to help - [email protected] Follow me- https://twitter.com/meNiZaM ![enter image description here][1]

Updated on July 09, 2022

Comments

  • Nizam
    Nizam almost 2 years

    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)

  • Nizam
    Nizam almost 11 years
    System.exit() does not kill your app if you have more than one activity on the stack. Please check it.
  • CommonsWare
    CommonsWare almost 11 years
    @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.
  • Nizam
    Nizam almost 11 years
    @CommonsWare 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.
  • CommonsWare
    CommonsWare almost 11 years
    @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".
  • Nizam
    Nizam almost 11 years
    @CommonsWare: Please correct me if I'm wrong. See the answer above. I used "killed" instead of "exit" there.
  • android developer
    android developer almost 4 years
    @CommonsWare 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.
  • CommonsWare
    CommonsWare almost 4 years
    @androiddeveloper: "Isn't it very un-recommended to use System.exit() on Android anyway? " -- yes.
  • android developer
    android developer almost 4 years
    @CommonsWare Very odd that the docs about it doesn't say it. I think it could be deprecated, even.
  • Tenfour04
    Tenfour04 almost 4 years
    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.
  • Sourav Kannantha B
    Sourav Kannantha B about 3 years
    @CommonsWare 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.
  • CommonsWare
    CommonsWare about 3 years
    @SouravKannanthaB: If you are using a dependency inversion framework (Dagger+Hilt, Koin, etc.), they usually have a facility to reset your managed singletons.