3

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?

The Vee
  • 11,420
  • 5
  • 27
  • 60
G.ONE
  • 507
  • 1
  • 5
  • 14
  • It may be help you. Please try to search. [link](http://stackoverflow.com/a/26882533/6005977) – Ankita Shah Oct 17 '16 at 11:59
  • [Here is the full descrption of your question](http://stackoverflow.com/questions/19897628/need-to-handle-uncaught-exception-and-send-log-file) – noobEinstien Oct 17 '16 at 12:28

4 Answers4

5

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 application manager.

onDestroy() may or may not be called on any given activity or service. The general rule is that either onDestroy() is called, or your process is terminated, or your code crashed.

How to delete data when app is closed ?

You don't. Your process may be terminated for any reason, at any time, by the user or by the OS. You may or may not be called with onDestroy() when that occurs. While you may be able to improve your success rate a little via onTaskRemoved() on a Service, this itself has proven unreliable, and it will not cover all scenarios.

If you do not want data to be in files when your process is terminated, do not create the files in the first place. Just hold onto the data in memory.

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

A Service method onTaskRemoved will be called when we remove app from recent items.

Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
0

I have checked when I close the application from the Task Manager forcefully the onDestroy() method is called. I tested like this:

  1. Minimize the Application using home button --> this calls

    onPause()
    onStop()
    
  2. Remove the app from Task Manager, then onDestroy() is called for that MainActivity (launcher).

There is no concept of exiting the application in Android.

How you can maintain the cache values, if you're using shared preferences, you should clear shared preferences values when the user logs out, not when the application closes.

In Android application is not closed until you remove it from the stack forcefully. Just a particular activity is destroyed.

The best practice is when the user logs in, store the credentials into shared preferences or global variables. When the user logs out from the particular activity, clear the session or cached values.

If you want to clear the cached values when the application is completely closed, use global variables (static variables) instead of shared preferences.

class GlobalValues{
    public static String userName;
    public static String password;
}

The static variables are destroyed when the application process is completely destroyed.

The Vee
  • 11,420
  • 5
  • 27
  • 60
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
0

If we need to clear the data from app, then use Service and if you remove application from recent apps, onTaskRemoved() will be called in service. So in onTaskRemoved() write your code to clear session data, manage resources etc.

shyam.y
  • 1,441
  • 1
  • 16
  • 17