A lightweight background manager for Android to know whether your app is in the background or foreground
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
background-manager
gradle/wrapper
.gitignore
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

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