Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Will finish() ALWAYS call onDestroy()? [duplicate]

Simple question: can you be sure that finish() will call onDestroy()? I haven't found any confirmation on this.

like image 550
Xander Avatar asked Nov 10 '13 16:11

Xander


People also ask

Does activity finish call onDestroy?

Third, finish() does not call onDestroy() . You can tell that by reading the source code.

Why onDestroy is not called when finished?

onDestroy() should be called at the end because then your code will be called before it is destroyed.

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

Does finish call onPause?

Android will generally call onPause() if you call finish() at some point during your Activity's lifecycle unless you call finish() in your onCreate() .


1 Answers

Simple question: can you be sure that finish() will call onDestroy()?

First, this answer assumes that you are referring to Android's Activity class and its finish() method and onDestroy() lifecycle method.

Second, it depends upon your definition of "sure":

  • Your process could be terminated in between finish() and onDestroy(), for reasons independent of whatever is triggering the call to finish()

  • A device manufacturer or ROM modder could introduce some screwy change that would break the connection between finish() and onDestroy()

  • The battery could go dead in between finish() and onDestroy()

  • Etc.

Third, finish() does not call onDestroy(). You can tell that by reading the source code. finish() usually triggers a call to onDestroy().

Generally speaking, finish() will eventually result in onDestroy() being called.

like image 192
CommonsWare Avatar answered Oct 07 '22 14:10

CommonsWare