2

Hi I've got an app with a code size of approximately 1/2mb. The app includes a webview for showing several pages. My cache therefore ends up at 2,5mb. Not much but enough. How can I clear my cache onDestroy?

Thx!

Felix ZY
  • 674
  • 6
  • 14
  • check this question: http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache – Buda Gavril Jan 09 '12 at 11:02
  • Thx for the link. I think it will help many people, but unfortunately... I didn't really get a hold of it. :( Is there a way to purge all cache for the application, not only the WebView on the OnDestroy(); event? – Felix ZY Jan 09 '12 at 19:49

2 Answers2

9

put this code in onDestroy() for clear app cache

@Override
    protected void onDestroy() {
        super.onDestroy();

        try {
            trimCache(this);
           // Toast.makeText(this,"onDestroy " ,Toast.LENGTH_LONG).show();
        } 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();
     }
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Chirag
  • 2,321
  • 24
  • 36
2

As given in the link provided above, you can get the cache directory and then delete it along with its children to clear the cache as follows:

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


    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; 
        } 
       } 
     } 
    return dir.delete(); 
   }
DarkKnight
  • 37
  • 4
  • So sorry for not saying this b4, but thank you for helping! At first, I didn't see the point of just clearing mWebView, but after a bit of testing I realized that mWebView was the cache source, and now it's all working like a charm. I've really just included mWebView.ClearCache(); (think that's right? haven't checked that piece of code with my actual project.) in the onStop event of all my webview using activities. – Felix ZY Mar 05 '12 at 20:41
  • return dir.delete(); could fail if dir is null. So rather move it into first if and then return false at the end. – slott Jan 23 '15 at 10:12