سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

No you cannot be sure!

Calling finish() generally triggers onDestroy() as per the Activity life cycle but you cannot rely on it. Specifically not for saving your data. Documentation clearly says

do not count on this method being called as a place for saving data! 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top