I was wondering what could be the reason for not having a callback at the application level when an application goes to background and comes to the foreground. Activity
class's onPause
and onResume
are only called on the current top activity. If I want to stop some background task that has the application level scope, then there is no easy way I can stop it when the app goes to background. There is high demand for these event callbacks.
Why doesn't Android have a app level callback on pause and resume of applications? Can it be implemented in Android at the task(activity stack) level if not at app level?
The real problem :
A background Timertask refreshes UI with data from web at regular intervals. When app is no longer in foreground I want to stop it.
Currently I am putting the repeating code in a BaseActivity
. This is at the activity level. The task stops and starts on each pause and resume of every activity and the event of app going to bg or coming to fg is hidden among one of these events, which I cannot know. I wanted to know if there is a better way of doing it, I mean knowing when the app has stopped being visible to the user.