注销时如何清除android中的缓存



我的应用程序有问题,当用户登录时,它会在Listview中显示他的数据,而且我正在将他的数据保存到缓存中,这样我可以最大限度地减少用户的请求,除非他刷新Listview,否则会有另一个请求,它会清除缓存。

但我遇到的问题是,当用户注销,另一个用户用他们正在使用的同一部手机登录该应用程序时,将要登录的新用户将看到前一个用户拥有的数据,这是因为缓存,所以新用户需要滑动刷新它来清除缓存,他现在就可以看到他的数据了。

这是我注销时的代码。

public void logoutProfileUser(){
session.setLogin(false);
db_login.deleteUsers();
db_profile.deleteUserProfile();
// Launching the login activity
Intent intent = new Intent(getContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

我正在使用Volley保存缓存,当用户单击注销按钮时,是否可以清除缓存?任何帮助都将不胜感激。

public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
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();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}

最新更新