What is Activity.finish() method doing exactly?

前端 未结 12 1825
别那么骄傲
别那么骄傲 2020-11-22 16:16

I\'m developing android applications for a while, and followed a lot of posts about activity life cycle, and application\'s life cycle.

I know Activity.finish

相关标签:
12条回答
  • 2020-11-22 16:39

    calling finish in onCreate() will not call onDestroy() directly as @prakash said. The finish() operation will not even begin until you return control to Android.

    Calling finish() in onCreate(): onCreate() -> onStart() -> onResume(). If user exit the app will call -> onPause() -> onStop() -> onDestroy()

    Calling finish() in onStart() : onCreate() -> onStart() -> onStop() -> onDestroy()

    Calling finish() in onResume(): onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()

    For further reference check look at this oncreate continuous after finish & about finish()

    0 讨论(0)
  • 2020-11-22 16:41

    It seems that the only correct answer here so far has been given by romnex: "onDestroy() may not be called at all". Even though in practice, in almost all cases it will, there is no guarantee: The documentation on finish() only promises that the result of the activity is propagated back to the caller, but nothing more. Moreover, the lifecycle documentation clarifies that the activity is killable by the OS as soon as onStop() finishes (or even earlier on older devices), which, even though unlikely and therefore rare to observe in a simple test, might mean that the activity might be killed while or even before onDestroy() is executed.

    So if you want to make sure some work is done when you call finish(), you cannot put it in onDestroy(), but will need to do in the same place where you call finish(), right before actually calling it.

    0 讨论(0)
  • 2020-11-22 16:42

    finish () just sends back to the previous activity in android, or may be you can say that it is going one step back in application

    0 讨论(0)
  • 2020-11-22 16:43

    Finish() method will destroy the current activity. You can use this method in cases when you dont want this activity to load again and again when the user presses back button. Basically it clears the activity from the.current stack.

    0 讨论(0)
  • 2020-11-22 16:48

    Also notice if you call finish() after an intent you can't go back to the previous activity with the "back" button

    startActivity(intent);
    finish();
    
    0 讨论(0)
  • 2020-11-22 16:49

    onDestroy() is meant for final cleanup - freeing up resources that you can on your own,closing open connections,readers,writers,etc. If you don't override it, the system does what it has to.

    on the other hand, finish() just lets the system know that the programmer wants the current Activity to be finished. And hence, it calls up onDestroy() after that.

    Something to note:

    it isn't necessary that only a call to finish() triggers a call to onDestroy(). No. As we know, the android system is free to kill activities if it feels that there are resources needed by the current Activity that are needed to be freed.

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