Question

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.

Was it helpful?

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.

OTHER TIPS

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.

No, there is no guarantee onDestroy() will be called.

The same goes for onStop()

What you want instead is onPause()

This is true for all versions of Android, going back to 1.0 to the very latest. The Activity lifecycle can be initially very confusing for beginners. But it's not the beginner's fault, it's the fault of the framework designers.

Another mistake beginners make is to assume that onResume() is about resuming the Activity, when in fact it's the UI thread that is resuming while it is in its Activity. This is another point that the documentation goes over and over again, but really, it would have been a lot simpler if the android team had called that method something less ambiguous to begin with.

according to: http://developer.android.com/reference/android/app/Activity.html Will allways be called,
onDestroy():
the final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top