simon.schvartzman wrote: ↑Sun Jul 29, 2018 6:40 pm
@jacque, let me ask you something, is there any way to trap the App Switcher? I have this app where the user is constantly sending his location to a server. I'd like to be able to identify when the user closes the app even if he does so via the App Switcher. Any ideas as how can I accomplish this?
I wish we could, but no messages are sent so there is nothing to trap. What we need are suspend and resume messages and there is a request for that, but apparently it is difficult to implement and so far there is no progress. Mark Waddingham said a long time ago that they did attempt it but couldn't make it work.
Due to the fact that Android never quits an app, it isn't really "suspended" or quit unless you specifically call the "quit" command. Android OS handles memory usage and if an app requires more free RAM that is currently available Android will unceremoniously dump background apps as needed. Your app can disappear at any time, so the only solution (and the way Android says you should handle things) is to constantly save the current state of the app so that it can resume where it left off if that is important to its functioning.
If the app does remain in RAM then it will automatically pick up where it was before, so you really only need to reinstate everything on a preOpenStack or openStack handler (when a suspended app resumes, there are no pre- or open- messages because nothing has changed.) But you do need to save the state repeatedly in case the app needs to restart.
You can see how it works by launching your app, going back to the launcher, and then immediately reopening the app. Unless your launcher does something RAM-intensive, your app should still be there just as it was. Now open Chrome browser and load a number of different web sites (which usually takes enough RAM to quit background processes.) Then reopen your app and chances are it will load as new again.