Just explanations, the answers work correctly, except that you need to pass the application context to trimCache instead of the Activity context (to avoid memory leak), because trimCache is a static method.
@Override protected void onDestroy() { super.onDestroy(); try { trimCache(getApplicationContext());
Otherwise, however, 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) {
source share