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
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)
finish()
is faster.(seems like more background operations are there in exit())So,
what's the difference between two?
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)
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.
exit function has status code, which tells about the termination, such as: exit(0) : Indicates successful termination. exit(1) or exit(-1) or any non-zero value – indicates unsuccessful termination.
return statement is used inside a method to come out of it. System. exit(0) is used in any method to come out of program. System.
Status code other than 0 in exit() method indicates abnormal termination of code. goto does not exist in Java, but it supports labels. It is better to use exception handling or plain return statements to exit a program while execution.
Actually there is no difference if you have only one activity. However, if you have several activities on the stack, then:
System.exit(0)
is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityAAccording 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With