Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to clear application cache memory and data on exit of application...
i used following code but it is not executed....please guide me..

Java
@Override
 protected void onDestroy() {
      // TODO Auto-generated method stub
  super.onDestroy();
  Log.i("ondestroy", "message");
      try {

           trimCache(getApplicationContext());
        } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
    }
  }

   public static void trimCache(Context context) {
          try {
              Log.i("trimcache", "message");

             File dir = context.getCacheDir();
             if (dir != null && dir.isDirectory()) {
                deleteDir(dir);
             }

          } catch (Exception e) {
             // TODO: handle exception
          }
       }



       public static boolean deleteDir(File dir) {
           Log.i("ondestry", "message");
          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();
   }
Posted
Updated 2-Jul-15 0:58am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900