0

I want to create a button in Android studio that should be part of the menu in my android WebView app, which automatically clears the cache of the application whenever a user clicks it.

How do I do that please? I am a beginner, so detailed explanation will be much appreciated!

John Doey
  • 1
  • 4
  • Does this answer your question? [Clear Cache in Android Application programmatically](https://stackoverflow.com/questions/23908189/clear-cache-in-android-application-programmatically) – ATP Jul 10 '22 at 14:04
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jul 10 '22 at 16:31
  • I don't have enough code – John Doey Jul 11 '22 at 03:12

1 Answers1

0

https://stackoverflow.com/a/23908638/16221912

add on click listener to your button view and call this deleteCache function

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        deleteDir(dir);
    } catch (Exception e) { e.printStackTrace();}
}

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();
    } else if(dir!= null && dir.isFile()) {
        return dir.delete();
    } else {
        return false;
    }
}
  • thanks for your reply, but how do I create the button view, before I even start talking about adding function? – John Doey Jul 10 '22 at 15:42