You can implement this by having a listener for "home" button, inside which the action is to something similar:
Keep in mind that there is no listner for home. But you can look for a OnStop()
protected void onStop()
{
super.onStop();
deleteFiles(cacheDir);
}
Within the listener call the deleteFiles(cacheDir);
private void deleteFiles(File dir){
if (dir != null){
if (dir.listFiles() != null && dir.listFiles().length > 0){
// RECURSIVELY DELETE FILES IN DIRECTORY
for (File file : dir.listFiles()){
deleteFiles(file);
}
} else {
// JUST DELETE FILE
dir.delete();
}
}
}
Permissions:
android.permission.CLEAR_APP_CACHE
android.permission.DELETE_CACHE_FILES
Refer:How can I clear the Android app cache?