I found that onDestroy()
is called only when back button is pressed, not when app is closed using recent apps (by pressing on cross or sliding the app) or in the Application Manager.
Below is my code in onDestroy()
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
((ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE))
.clearApplicationUserData();
}
I wrote the code to delete data and cache when onDestroy()
is called, so if someone presses the Back button, the data and cache clears and app closes (instead of going to the background) but when I close it from Recent Apps or App Manger the data and cache remain.
How to delete data when my app is closed?