Search code examples
androidondestroyactivity-finish

Does onDestory() is ensured to be called after finish()?;


I always think finish() will fire onDestory() immediately.But in 4.3 it seems not the truth. I just want to know in which condition it'll happend ? sorry,I can't put all my code on here. It confused me a few hours,and I can't find any useful info about it.


Solution

  • From the docs:

    onPause() is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage.

    It doesn't matter if you used finish() or the system killed your Activity on its own. If the system "wants" to recover memory, there's no guarantee that onDestroy() or onStop() will be called.