My experience shows that most applications really don't need the exit callback you describe.
The Android method, which usually works fine, relates to the component (Activity, Service ..) of the life cycle management.
In some applications, the "Exit" function is provided (via a button, menu, etc.), which, when activated by the user, the application allows you to close all open components and basically go down.
But, if exit-callback is really what you want, the closest to it is probably to create a specialized service without any logic other than the onDestroy () function and activate it when the application starts, without closing it!
class ExitListenerService extends Service { @Override public void onDestroy() { super.onDestroy(); // App exit logic here. Not deterministic! } }
Most likely, such a service is likely to be the last component that will be fixed by the operating system. This should work in most cases, it works fine for me. But this is not 100% guaranteed.
But .. if you should have a bulletproof solution, the only other way I know is to create a peer-to-peer application, call it a watchdog that will wake up periodically to check the weather or not to use the main application, it still works and if it doesn't work activates the exit logic.
To complete this last check, you will need to call
List<RunningAppProcessInfo> runningApps = activityManager.getRunningAppProcesses();
and iterating over runApps that are looking for your own.
Gilad haimov
source share