Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to detect application shutdown?

Tags:

android

I have an app that automatically transmits location data to servers in the background (yes, this is necessary) but needs to do it while the activity is destroyed to save memory (i.e. onDestroy() won't work) but I want my app to terminate these operations when "swiped away" in the running applications view.

Is it even necessary to do this or will threads be interrupted automatically?

EDIT: I learned about services myself, but I am going to award the bounty to the person with the most comprehensive example of using them. (I don't need it myself but I think the current answers are not worthy of a bounty.)

like image 752
noɥʇʎԀʎzɐɹƆ Avatar asked Aug 10 '15 13:08

noɥʇʎԀʎzɐɹƆ


1 Answers

yes, this is necessary) but needs to do it while the activity is destroyed to save memory

I got a feeling you are doing something wrong. Why is sending location to remote peer coupled with activity lifecycle? I do not see any sense of doing that. If you want to send location only when you are in foreground you can utilise ActivityLifecycleCallbacks so you can be notified when you went into foreground or background and stop your separate sending worker when you go into background or resume when your activity become front-most one.

As for recent list related question, let me paste this comment by Dianne Hackborn:

what specifically happens when you swipe away a recent task is it: (1) kills any background or empty processes of the application (see http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#Lifecycle for what this means), and (2) uses the new http://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent) API to tell any services of the application about the task being removed so it can do whatever it thinks is appropriate.

you can find this post here: https://plus.google.com/105051985738280261832/posts/GfwRYCC42uX (I failed to find the way to link directly to the comment on google plus thing).

like image 132
Marcin Orlowski Avatar answered Sep 30 '22 11:09

Marcin Orlowski