I have a button "Logout", and after click them i want to clear all data and cache of app. I found in similar topics in this site methods to delete data:
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if(appDir.exists()){
String[] children = appDir.list();
for(String s : children){
if(!s.equals("lib")){
deleteDir(new File(appDir, s));
}
}
}
}
public 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();
}
But when i use the code:
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearApplicationData();
finish();
startActivity(new Intent(ProfileActivity.this, MainActivity.class));
}
});
my app restarts, but app data doesnt delete. So, how to delete programatically all cache and data of app?