14

What I want to do is to clear the cache memory of application on exit of application.

this task i can do manually by this steps.

< Apps --> Manage Apps --> "My App" --> Clear Cache>>

but i wants to do this task by programming on exit of application.. please help me guys..

Thanks in advance..

Arun Joshi
  • 170
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [How to clear cache Android](http://stackoverflow.com/questions/6898090/how-to-clear-cache-android) – Gofurs Oct 31 '16 at 17:06

3 Answers3

18

Try this one -

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class HelloWorld extends Activity {

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle *) {
      super.onCreate(*);
      setContentView(R.layout.main);
   }

   @Override
   protected void onStop(){
      super.onStop();
   }

   //Fires after the OnStop() state
   @Override
   protected void onDestroy() {
      super.onDestroy();
      try {
         trimCache(this);
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   public static void trimCache(Context context) {
      try {
         File dir = context.getCacheDir();
         if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
         }
      } catch (Exception e) {
         // TODO: handle exception
      }
   }

   public static boolean deleteDir(File dir) {
      if (dir != null && dir.isDirectory()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
               return false;
            }
         }
      }

      // The directory is now empty so delete it
      return dir.delete();
   }

}

Refer these links -

Community
  • 1
  • 1
Praveenkumar
  • 25,445
  • 23
  • 94
  • 171
  • 2
    Do not remove files on the main thread! You must do these calls on some other thread to avoid ANRs! – Jona Aug 23 '16 at 20:48
13

To clear Application Data Please Try this way. I think it help you.

public void clearApplicationData() 
{
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) 
{
    if (dir != null &amp;&amp; dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}
Praveenkumar
  • 25,445
  • 23
  • 94
  • 171
Md Abdul Gafur
  • 6,169
  • 2
  • 26
  • 37
  • Thanks for such a wonderful answer. but my concern is that when i should call this method because i am having lots of confusion on timing of calling of this method – Arun Joshi Jun 11 '12 at 09:37
  • 2
    In your application there will be an activity in which the user exits(usually the main activity), override the OnDestroy() and call the above clear cache code. – Sathesh Oct 20 '13 at 09:43
3

Just a clarification, the answers work properly except you have to pass the Application context to trimCache instead of the Activity context (to avoid memory leak), since trimCache is an static method.

 @Override
   protected void onDestroy() {
      super.onDestroy();
      try {
         trimCache(getApplicationContext()); //if trimCache is static
      } catch (Exception e) {
        e.printStackTrace();
      }
 }

However, otherwise, you can make trimCache non-static and there is no need to pass any Context.

public void trimCache() {
   try {
     File dir = getCacheDir();
     if (dir != null && dir.isDirectory()) {
        deleteDir(dir);
     }
  } catch (Exception e) {
     // TODO: handle exception
  }
}
Rod
  • 105
  • 2
  • 8