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
}
}