views:

96

answers:

3

Hi,

I understand when an activity closes, onDestroy() is called. But this is not done always right? Sometimes, onPause() is called.

So suppose I want to clear some memory when an activity closes, where exactly do I do it? Since onDestory may not be called, I cannot keep it there either right?

Elaborating: I have 2 activities A1 and A2. A1 is hsown in the startup of the app. A1 calls A2 later. Suppose I create a class object in onCreate() of Activity A1. This object must be deleted when the I exit the app, i.e when the app is no longer visible. Is the best place to do this onDestroy() or onStop() of A1? I guess onPause() may not be the right place, because onPause() will be called when A1 calls A2 and I dont want to delete the object then.

-Kiki

+1  A: 

Google's docs say to do things that need to be done before exit in onPause(), because yes, in low memory situations onDestroy() may not be called.

ShadowGod
Hmm, but onPause of an activity will be called if it moves to another activity on the same app. I want the memory to be released only when the app exits, i.e say like press back button frm that activity.
kiki
I understand. All I am saying is that I believe Google has said that onDestroy() may not always be called in low memory situations. But onPause() always will be called when leaving an activity. Back button doesn't always (if ever?) call onDestroy() but the Home button or calling finish() does. As far as I know. Just trying to help but I'm no expert so if I'm wrong maybe some more advanced people could jump in here and correct me.
ShadowGod
+1  A: 

I don't think that onSuspend() exists. It is not mentioned inside the documentation. Maybe you are referring to onStop().

When onStop() is called, the activity is no more visible but not yet destroyed. The termination really takes places after the onDestroy() call, whether it is terminated by a call to finish() or memory requirements from Android as the documentation says. Then, the right place to free memory in my opinion is onDestroy().

If you have to stop CPU-intensive operations that are related to user interaction, do it in onPause().

DavLink
I meant onPause, sorry. But will onDestroy be called always? Is it safe?
kiki
+1  A: 

I think you mean onPause(), there is no onSuspend() method. If your activity closes cleanly, it will call onStop() and onDestroy().

If the system is running low on memory and wants to kill your activity, then onPause() is guaranteed to be called before your process is killed, but that's the only guarantee. The methods onStop() and onDestroy() may not be called. So you should cleanup in onPause().

However... don't forget that your activity can transition many times between onResume() and onPause(), so you don't want to do too much allocations and cleanups in those two methods, they should be quick.

You have to decide how best to cleanup in onPause(), and what you actually have to do, if your activity needs to do something before it is killed off during low memory situations.

codeboy2k
Yes, I meant onPause(), not onSuspend(). Have editted the question. Could you check again? Because I think onPause() wont suit my scenario.
kiki
memory allocated in onCreate() should be released in onDestroy(). This keeps memory free when your activity is being created and destroyed in the normal course of an activity's life. The only time when onDestroy() will not be called is in a low memory situation, when your activity is being killed to free up memory. In this case, it doesn't matter, because your entire process is going away, and that includes the VM and all the memory it gave you in A1. It's not going to be important at all, unless A1 needs to inform some other activity that it's going away, for this you need to use onPause()
codeboy2k
Additionally,if you do need to inform another activity that you're going away, then the only message you can send is from within onPause(), and you can tell the other activity that "I'm pausing, don't rely on me anymore"... that's about it. If another activity might hang because A1 goes away without telling the other activity, then you need to consider this and program it in. If it's just memory you're worrying about,then don't worry,it's not important in this scenario. write it correctly, to free memory in the onDestroy(), and also free it when you don't need it anymore, esp. if it's large
codeboy2k