简体   繁体   中英

How can i clear cache in android when logout?

I am having a problem with my application, when the user login, it will display his data in the Listview, and also i am saving his data to the cache so that i can minimize the request of the user unless he will swipe refresh the Listview then there will be another request and it will clear the cache.

but the problem that i am having is when the user logout, and another user login to the app with the same phone that they are using, the new user that will login will see the data that the previous user have, it is because of the cache, so the new user needs to swipe refresh it to clear the cache and he will be able to see his data now.

here is my code in logout.

    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);
    }

and i am using Volley in saving cache, is it possible to clear the cache when the user click the logout button? any help will be really appreciated.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM