Android: Will finish() ALWAYS call onDestroy()?

Xander picture Xander · Nov 10, 2013 · Viewed 31.3k times · Source

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

Answer

CommonsWare picture CommonsWare · Nov 10, 2013

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.