Skip to content

A lightweight background manager for Android to know whether your app is in the background or foreground

Notifications You must be signed in to change notification settings

kirtan403/BackgroundManager

Repository files navigation

BackgroundManager

A lightweight background manager for Android to know whether your app is in the background or foreground

When at least one of your app's activity is in the foreground, your app is assumed to be in the foreground. And when none of your activities are in the foreground, your app is assumed to be in the background. BackgroundManager keeps track of no of activities in the foreground, and decides if an app is in the foreground or background.

Inspired from Doug's commit here and from my own similiar approach here.

What can you do with this?

  • Check if your app is in the background or in the foreground
  • Add listeners which will be called whenever your app switches between background and foreground

How can I do this?

First add dependency:

In project level gradle file:

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }  // Add this line
    }
}

In your app build.gradle file:

dependencies {
    compile 'com.github.kirtan403:BackgroundManager:1.1.0'
}

Now, initialize BackgroundManager in your Application class in onCreate:

    BackgroundManager.init(this)   // Initialize

Done! Now you are good to go.

How can I check if my app is in the background or foreground?

There are 2 ways of doing this.

  1. With isBackground() or isForeground() methods

Simply call BackgroundManager.isBackground() to know if app is in the backgorund or not. Similarly BackgroundManager.isForeground() to know if app is in the foreground or not.

  1. With listeners

You can attach listners and get call back whenerver your app transits between background and foreground.

To attach a listener you can call BackgroundManager.addBackgroundStateChangeListener(mListener)

And your listener will have isBackground() and isForeground() methods. Which will be called based on the new state of the app.

That's it, folks!

#BuildBetterApps

About

A lightweight background manager for Android to know whether your app is in the background or foreground

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published