1

I have some utility code in my android application running as part of a shared component. It's scoped to the lifetime of the application, and launched as part of an overridden Application class.

What I'd like to know is if I can be notified when the application itself enters or leaves the foreground - basically the equivalent of iOS' applicationDidEnterBackground or foreground.

I've done a variety of searches, but everything comes back with people saying you should hook onPause and onResume on an activity - This would be great if my app only ever had one activity, however it has lots, and it doesn't seem sensible to hook onPause/Resume on every single one - let alone handling transitions between my activities

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
  • Unfortunately (AFAIK) android doesn't provide us with such apis that would tell us if application is leaving the foreground or not. What you can do is create a BaseActivity and have all other activities extend from it and then in the BaseActivity you override onpause/start/etc to keep track of app state – denvercoder9 Mar 20 '18 at 09:12
  • There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged and onStop. – AbuQauod Mar 20 '18 at 09:14
  • You can create BaseActivity and extends this activity in every Activities.Now you can handle only one activity onPause() and onResume() method. – Saurabh Vadhva Mar 20 '18 at 09:14
  • Possibly duplicate with: https://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service – Cao Minh Vu Mar 20 '18 at 09:14
  • the application has nothing to do with foreground or background. That 2 terms can't be applied to the application. What you probably want ti to check if all the activities of your application are in background. And here onPause and onResume of the Activity come in. So there is no way except manually storing the state of all activities. If I needed it, I'd store a map of `` in the Application instance, and in Activity onPasue, onResume I'd change this state for current activity. – Vladyslav Matviienko Mar 20 '18 at 09:15
  • if you want to handle it once for all then you have to create your custom activity and extend all activity from your custom one – AbuQauod Mar 20 '18 at 09:15
  • This has some nice solutions: https://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background – denvercoder9 Mar 20 '18 at 09:17

1 Answers1

2

There isn't any direct approach to get the application status while in the background or foreground, but you can register your application class to the Activity Lifecycle Callbacks, just add your listener to the application like this:

myApplication.registerActivityLifecycleCallbacks(yourCallback);

and you will be able to know if you have activity on the foreground.

  • You can combine both answer posted here, the first using some sort of base class , which during lifecycle store state somewhere, and later , register activity lifecycle callbacks to Application object. In the past you were enable to getRunningTask without permission and get Activity that stays at top in a given time, however this was deprecated in API 26. You still can use getProcessesInfo also from ActivityManager as well, but you should figure out a way to reasonimg regard the state, some alive doesn't mean visible in the stack... – william gouvea Mar 20 '18 at 09:54