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

江枫思渺然 提交于 2019-12-17 06:11:57

问题


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)


回答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



回答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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/18292016/difference-between-finish-and-system-exit0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!