简体   繁体   中英

how do I clear the device memory cache

In my android app, I am using transition scenes. On the Emulator, it works fine but on a physical device the more times a transition happens, the time between the transition grows, slowing down the app. I need to clear the device cache memory to stop this from happening.

I tried a code to clear the cache memory, but the android studio gives out a warning that the delete code will be ignored. I have not found any other way to do this.

public void clearCache() {
Log.i(TAG, "Clearing Cache.");
File[] dir = mContext.getCacheDir().listFiles();
if(dir != null){
    for (File f : dir){
        f.delete();
    }
   }
}

The delete() method call is ignored by the system so the code doesn't work. I need to clear the device cache can someone help.

android studio gives out a warning that the delete code will be ignored that's wrong, it does not give such warning.

It says that the result of f.delete() is ignored.
f.delete() returns true / false as the result, which means

  • true if file has been deleted
  • false if file deletion failed.

You may ignore this warning, if you don't care about the delete() result, or check it like

for (File f : dir){
    if(!f.delete()) {
        //deletion failed. Do something about it
    }
}

If you just want the warning to disappear, you can do something like

for (File f : dir){
    boolean ignored = f.delete();
}

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