Orion Edwards
Orion Edwards

Reputation: 123672

How to tell when an Application (not activity) enters the background?

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

Upvotes: 1

Views: 100

Answers (1)

Refael Nahum
Refael Nahum

Reputation: 36

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.

Upvotes: 2

Related Questions