2

I have seen other answers on stackoverflow but none of them helped me solve my issue.

I have an application with webview in a activity, I want to clear cache when the application is killed.

Below is the code I'm using to delete the cache-

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

I want to clear cache when application is killed or just before the app is going to be killed, where do I call deleteCache(this)?

I tried putting it in OnDestroy() method of the activity , but it clears the cache when I press back button on the activity which is not my requirement.

So I added

 @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

So my activity doesn't call onDestroy() at all when I press back button nor I kill the application.

Where do I need to clear the cache on the lifecycle methods ? and Where can I trigger an event to javascript function just before my app is killed?

Onik
  • 19,396
  • 14
  • 68
  • 91
DroidDev
  • 789
  • 9
  • 19
  • Try this. http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache – Alvin Varghese Apr 25 '17 at 06:36
  • @AlvinVarghese That question was more of not using cache at all in the application. So he clears cache when the webview loads. Not in my case, I want clear it when the application is destroyed. – DroidDev Apr 25 '17 at 06:39
  • 1
    You won't get any callbacks invoked if the system "decides" to kill your app process being in background. If you, as a developer, can't rely on that, maybe you should rethink the approach to the problem?... What you know for sure is that your app process is created the [`Application`'s `onCreate()` method is called](http://stackoverflow.com/questions/43101071/application-lifecycle-when-app-is-killed-in-the-background/43101601#43101601). So why not clear the cache from within the method, i.e. not upon the app killing, but upon its launch? – Onik Apr 25 '17 at 19:25
  • @Onik Thanks for your input it makes sense but I followed this approach [OnClearService](http://stackoverflow.com/questions/21040339/how-to-know-when-my-app-has-been-killed) and created a service and cleared my cache at onTaskRemoved(); – DroidDev Apr 25 '17 at 19:44
  • 1
    _"...created a service and cleared my cache at onTaskRemoved()"_ A good approach! But, from my experience, in rare cases `onTaskRemoved()` isn't guaranteed to be called. I recommend to test the method call for different OS versions (and ideally for a range of devices). Additional notes on the method call can be found [here](http://stackoverflow.com/questions/40072294/when-can-ontaskremoved-be-called/40072391#40072391). – Onik Apr 25 '17 at 19:51
  • 1
    @Onik Awesome, I'll test it out on some devices. Seems like I need to follow - Application's OnCreate() method approach, as you have already done hell lot of research on this . Thanks – DroidDev Apr 25 '17 at 20:04
  • You can try the below link to determine if you application is killed: https://stackoverflow.com/questions/21040339/how-to-know-when-my-app-has-been-killed – Aditi Apr 25 '17 at 06:43

2 Answers2

1

You won't get any callbacks invoked if the system "decides" to kill your app being in background. If you, as a developer, can't rely on that, maybe you should rethink the approach to the problem?...

What you know for sure is that when your app process is created the Application's onCreate() method is called. So why not clear the cache from within the method, i.e. not upon the app killing, but upon its launch?

I followed this approach and created a service and cleared my cache at onTaskRemoved().

Good approach, but, from my experience, in rare cases onTaskRemoved() isn't guaranteed to be called. I recommend to test the method call for different OS versions (and ideally for a range of devices). Also note that onTaskRemoved() is only called when a user removes your app from the recent task list.

To sum up, if you use Application's onCreate() to clear the cache, the operation is guaranteed to be performed.

Community
  • 1
  • 1
Onik
  • 19,396
  • 14
  • 68
  • 91
0

Just put this before leaving the activity

WebView.clearCache(true);

Faravy
  • 93
  • 1
  • 9
  • What do you mean by leaving the activity? You mean onDestroy() ? Please check my question. – DroidDev Apr 25 '17 at 07:29
  • yes u can put in onDestroy or write finish(), when u navigate to another activity – Faravy Apr 25 '17 at 07:31
  • OnDestroy will be called when I press backbutton. I want to clear cache when I kill the application. Again please check my question ! – DroidDev Apr 25 '17 at 07:32
  • if your webview activity is the last activity of ur app then it will work. Otherwise u can use a service before killing the app just stop the service. – Faravy Apr 25 '17 at 07:35