3

Is there a decent way to detect through a android shared library when the app that references the library has gone into the background/foreground? The library has no access to any activities whatsoever, asides from Context.

i.e Can the library register a broadcast receiver to receive such events?

kkudi
  • 1,625
  • 4
  • 25
  • 47
  • How about this way: http://stackoverflow.com/questions/15487754/androidhow-to-check-if-application-is-running-in-background-2 You'll need the library do depend on Android SDK though. – ohaleck Oct 09 '13 at 21:58
  • the library is fine to depend on the SDK, however I'm not sure I want to do this on demand. Ideally, once the library is initialised, I would like to be able to handle the events automatically (i.e has the app resumed again). I'm not sure if this is possible hence the question here :-) – kkudi Oct 09 '13 at 22:01
  • 2
    The common way would be to have some code in `Activity#onPause` and `Activity#onResume` notify the library code of application state changes. If you create an abstract `Activity` subclass that does just that as a part of your library and make all other activities extend it, you'll save yourself a code duplication. – ohaleck Oct 09 '13 at 22:15
  • Yes, some sort of callback that the library would handle I guess. I guess I was asking mostly because the library is meant for other developers and I would want to minimise the code they need to write for my library to work. Thanks for the confirmation. – kkudi Oct 09 '13 at 22:19

0 Answers0