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

后端 未结 4 1459
醉话见心
醉话见心 2020-11-28 11:46

I\'m talking about programming in android.

In early days I thought that, finish() closes current activity and go back to the previous

相关标签:
4条回答
  • 2020-11-28 12:21

    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
    0 讨论(0)
  • 2020-11-28 12:25

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-28 12:33

    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.

    0 讨论(0)
提交回复
热议问题