Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

I want to clear my application cache memory when it set to exit.
I want reference/help/code that handles my application.

Thanks in advance.
Posted
Updated 16-Oct-19 0:11am

Remove Cache

fun deleteDir(dir: File?): Boolean {
if (dir != null && dir.isDirectory()) {
val children = dir.list()
for (i in children.indices) {
val success = deleteDir(File(dir, children[i]))
if (!success) {
return false
}
}
return dir.delete()
} else return if (dir != null && dir.isFile()) {
dir.delete()
} else {
false
}
}
 
Share this answer
 
v2
Comments
Richard Deeming 16-Oct-19 15:41pm    
An unexplained, unformatted code-dump, in a different language to that used in the question, is not a "solution".
OnStop Event place the code to clear cache
C#
Java.Lang.Runtime.GetRuntime().RunFinalization();
Java.Lang.Runtime.GetRuntime().Gc();
trimCache(this.ApplicationContext);





C#
public void trimCache(Context context)
       {
           try
           {
               Java.IO.File dir = context.CacheDir;
               if (dir != null && dir.IsDirectory)
               {
                   deleteDir(dir);
               }
               context.DeleteDatabase("webview.db");
               context.DeleteDatabase("webviewCache.db");
           }
           catch (Exception e)
           {
               CostantsLift.WriteTextFile("   trimCache ", e.Message, currDate);
               // TODO: handle exception
           }
       }
       public bool deleteDir(Java.IO.File dir)
       {
           if (dir != null && dir.IsDirectory)
           {
               String[] children = dir.List();
               foreach (string child in children)
               //for (int i = 0; i < children.Length; i++)
               {
                   bool success = deleteDir(new Java.IO.File(dir, child));
                   if (!success)
                   {
                       return false;
                   }
               }
           }

           // The directory is now empty so delete it
           return dir.Delete();
       }
 
Share this answer
 
Please check this [LINK] for the code to clear cache on exit in android.

Cheers,
Sudhakar
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900