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
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()
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.
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
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.
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();
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.