Question

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

Was it helpful?

Solution

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
}

OTHER TIPS

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.

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?

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

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