9

I have put some cache cleaning code in onDestroy of my activity but most of the time the code is not executed unless I explicitly finish the activity via finish().

Edit: Just read onDestroy is called only with finish() or if the system is low on resources. So where do I need to put my cache cleaning code? If I put it in onPause() and the user goes back to the app, the cache is cleared. I am actually storing important temporary files in the cache that should not be deleted in onPause.

nemo
  • 55,207
  • 13
  • 135
  • 135
yeahman
  • 2,737
  • 4
  • 21
  • 25

3 Answers3

16

From Android developer documentation:

protected void onDestroy ()

Added in API level 1 Perform any final cleanup before an 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.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

You can move your code to onPause() or onStop()

meda
  • 45,103
  • 14
  • 92
  • 122
  • i store resized images in the cache.. if i flush it onstop()/onpause() and the app resumes, my resized images will be lost.. should i resize them again? it's pretty resource consuming – yeahman Oct 26 '13 at 16:47
  • It's very simple save your images `onStop()` then load them `onStart()` – meda Oct 26 '13 at 16:56
  • i am storing the resized images in a temporary cache folder... the user can either click on a button to save the images or leave the app using back button.... the problem is i want to clean/erase the resized images only when he leaves my app – yeahman Oct 26 '13 at 17:53
  • ok onPause is called when activity is not visible to the user, so you can put the code there , then restore it onResume. – meda Oct 26 '13 at 18:49
  • 3
    so you saying i delete all resized images in `onPause` and re-create them in `onResume`? it can happen that there are 500images.....each time it takes some time to resize them, it's not really user friendly imo – yeahman Oct 27 '13 at 06:32
  • @yeahman My post is an attempt to answer your question about `onDestroy()` not being called, maybe you should add more code to your post, or start a new question. It's hard to guess your app without code, I don't want to give you a bad advice you know what I mean. – meda Oct 28 '13 at 23:40
  • yup thx for the answer :) managed to recreate the resized images when needed thx – yeahman Oct 30 '13 at 19:37
  • Another stupid Android decision, nothing to see here. So it turns out you cannot have a single method which is called ONLY when the fragment is actually destroyed. See that onStop is called when the user puts the activity in the background too, that doesn't mean its destroyed. So stupid, who makes these decisions? – breakline Jan 22 '18 at 03:03
  • This question was for an Activity class, you should read up in [Fragment LifeCycle](https://developer.android.com/guide/components/fragments.html#Lifecycle) there is the method `onDetach()` – meda Jan 22 '18 at 13:20
3

try to use onstop

like this

@Override
    protected void onStop() {
        super.onStop();
       //write your code here
    }
Mohsen fallahi
  • 915
  • 1
  • 10
  • 27
1

ondestroy is mostly called when the system completely removes the activity from memory, or when a user kills the activity, you want to save your data in on pause, as that will always be called before the destroy.

Markko
  • 93
  • 6
  • 1
    onDestroy() is not called in the case of a force close. – Simon Oct 26 '13 at 17:50
  • Neither is it called when removing the app via swiping out of recent apps. At least most of the time, sometimes it triggers. I'm really wondering what exactly is supposed to go into this callback. – Zackline Feb 19 '16 at 10:36
  • You are right, very odd. "Here is any entry point to clean up resources before an activity is killed - maybe". – nmw01223 Aug 18 '21 at 07:31