0

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?

Alex D.
  • 1,424
  • 15
  • 40
  • "but app data doesnt delete" -- how did you determine this? – CommonsWare Oct 27 '16 at 14:59
  • data in database didnt delete. shared preferences value doesnt delete... – Alex D. Oct 27 '16 at 15:01
  • Please explain, **in detail**, every step you took to determine that "data in database didnt delete. shared preferences value doesnt delete". For example, did you comment out `startActivity()`, then look to see if the actual files are gone? Perhaps your new `MainActivity` instance is showing some data that you have cached in memory. – CommonsWare Oct 27 '16 at 15:07
  • SharedPreferences.Editor editor = preferences.edit(); editor.putString(V1_KEY, "someValue"); editor.apply(); then clearApplicationData(); finish(); Intent i = new Intent(ProfileActivity.this, MainActivity.class); startActivity(i); and after than i get the value preferences.getString(V1_KEY, null); and obtain "someValue" instead of null ... – Alex D. Oct 28 '16 at 12:43

1 Answers1

1

First, your code does not necessarily delete the file where SharedPreferences are stored. Its location is undocumented, IIRC, and it does not necessarily have to be in the scope of what you are deleting.

Second, SharedPreferences are cached in your process. Even if you do successfully delete the file, the cached SharedPreferences will not know that you deleted it. Until your process terminates, you will continue to have access to the "deleted" SharedPreferences data.

If your objective is to be able to delete data, only store data in places that you control, and where you control how that data gets cached. Replace your use of SharedPreferences with something else (e.g., SQLite database, or some file that you manage yourself).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491