How can I clear user data or clear cache using PhoneGap and Android? The code below does not work. Where should I make the changes; On the HTML side or on the Java side? Also I'm accessing an AJAX request and on the second attempt of a PUT method, the data doesn't update so my primary suspect is the user data which Android stores. I have added cache: false
on my AJAX request to ensure it does not store the cache. But it still does not work. Any ideas on how to fix this?
I have this code based from another question.
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
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();
}
So basically my POSActivity.java would look like this.
public class PocActivity extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.setIntegerProperty("splashscreen", R.drawable.is);
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
deleteCache(this);
super.loadUrl("file:///android_asset/www/index.html");
}
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
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();
}
}
Clear cache: Deletes temporary data. Some apps can open slower the next time you use them. Clear data storage: Permanently deletes all app data. We recommend trying to delete from inside the app first.
What Will Clearing Cached Data Do? Most devices have some form of cache cleanup. New data comes in, and older information is removed. This system ensures that your device isn't bogged down by so much storage that it can't tackle anything new.
Delete your browsing data If you sync a type of data, deleting it on your Android device will delete it everywhere it's synced. It'll be removed from other devices and your Google Account. Settings. Clear browsing data.
You can delete app data for any Android app with the same steps. Clear the app cache files of any app you think is causing performance issues, or delete app data for any apps you want to open with a clean slate. You'll get some storage space back, but nothing you've downloaded will be deleted.
Add below code in your oncreate() method of activity file after loading index.html :
if (super.appView != null) {
super.appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
I was looking for a easy way to clear the cache in my Cordova Application, and found this plugin which clears the webView cache in Android using the following code:
// clear the cache
self.webView.clearCache(true);
And in iOS, runs in background and do this:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
To use the plugin, simply call window.CacheClear(success, error);
Luckily for me, it solved the problem I had. Big thanks for the writer of this cordova-plugin-cache-clear!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With