2

I'm writing an app that allows the user to make a photo and then do some work with it.

I launch the camera with an IMAGE_CAPTURE intent, and provide a path to have the photo saved there.

This photo is temporary and I want to delete it when my app finishes.

I have an activity where I show the photo and allow the user to interact with it, and I thought the best way to delete it was on the onDestroy method.

The problem I am having is that when the user changes the orientation of the device, the activity is destroyed and replaced by a new one, but I cannot delete the file just because the user changes orientation as it is needed later on.

So, what can I do for keeping these files deleted?

Is there any other method i can overwrite, such as onAppExit, where I can delete all my temp files?

thanks in advance

richardtz
  • 4,993
  • 2
  • 27
  • 38

4 Answers4

2

I think the best way is to delete it in overrided onBackPressed() method of you activity. You see, back pressed event is the only exit point that means that user is done interactig with this activity instance and will newer return to it.

Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56
  • I was thinking the same, but if the application is finished while in another activity then the files will remain there – richardtz Sep 21 '12 at 11:21
  • Yes, and they should remain since user can start you application again later and will be abble to navigate back to you activity. And if you delete this file, activity will be unable to find it. – Dmitry Ryadnenko Sep 21 '12 at 11:22
  • Makes total sense, but I don't want these files to be there. If the app is restarted it should restart from the beginning (for the case of this app). – richardtz Sep 21 '12 at 11:30
  • Are you using System.exit(0) in you application? – Dmitry Ryadnenko Sep 21 '12 at 11:34
  • When what do you mean by "application is finished while in another activity"? How this can happen? User presses "Home" button? But in this case application isn't finished, it's just moved to background. – Dmitry Ryadnenko Sep 21 '12 at 11:42
  • Do not use System.exit(0), it should not be used under most circumstances. – slezadav Sep 21 '12 at 18:25
2

Always use getExtExternalCacheDir() API to get the cache directory on sdcard. Create all your temp files in this path. This cache will be destroyed after your app is uninstalled

Even better use getCacheDir() for cache dir path in local storage.. This memory will be automatically cleared when device is low on memory.

Here is a post that explains how to clear cache manually: How to delete cache-folder of app?

Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Android documentation doesn't says this files will be destroyed on app exit. It says they will e destroyed on app uninstall – Dmitry Ryadnenko Sep 21 '12 at 11:15
  • I am using that folder to create the files, but they remain there. – richardtz Sep 21 '12 at 11:16
  • I started using getCacheDir, but as I recall, these files are private to your application, and the app interacts with others (sending the file as attachment by mail) which don't have visibility on that internal cache dir, so I changed to getExternalCacheDir. – richardtz Sep 21 '12 at 11:23
1

So declare global boolean rotation and use this :

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {      
    super.onRestoreInstanceState(savedInstanceState);
    rotation=false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    rotation = true;
    super.onSaveInstanceState(outState);
}

Then in onDestroy() use

if(!rotation){
//delete here
}
slezadav
  • 6,104
  • 7
  • 40
  • 61
  • it works fine, my files are not being deleted just because a orientation change. But also, the onDestroy is not being called. I will test this approach together with the onBackPressed suggestion by boulder. – richardtz Sep 21 '12 at 11:55
0

You could check the isFinishing() method from within the onDestroy() as outlined in the accepted answer for this question How to distinguish between orientation change and leaving application android

Community
  • 1
  • 1
Dittimon
  • 986
  • 2
  • 14
  • 28