Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class for detecting and eventing whether an Android app is currently foreground or background (requires API level 14+)
package com.sjl.util;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Usage:
*
* 1. Get the Foreground Singleton, passing a Context or Application object unless you
* are sure that the Singleton has definitely already been initialised elsewhere.
*
* 2.a) Perform a direct, synchronous check: Foreground.isForeground() / .isBackground()
*
* or
*
* 2.b) Register to be notified (useful in Service or other non-UI components):
*
* Foreground.Listener myListener = new Foreground.Listener(){
* public void onBecameForeground(){
* // ... whatever you want to do
* }
* public void onBecameBackground(){
* // ... whatever you want to do
* }
* }
*
* public void onCreate(){
* super.onCreate();
* Foreground.get(this).addListener(listener);
* }
*
* public void onDestroy(){
* super.onCreate();
* Foreground.get(this).removeListener(listener);
* }
*/
public class Foreground implements Application.ActivityLifecycleCallbacks {
public static final long CHECK_DELAY = 500;
public static final String TAG = Foreground.class.getName();
public interface Listener {
public void onBecameForeground();
public void onBecameBackground();
}
private static Foreground instance;
private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;
/**
* Its not strictly necessary to use this method - _usually_ invoking
* get with a Context gives us a path to retrieve the Application and
* initialise, but sometimes (e.g. in test harness) the ApplicationContext
* is != the Application, and the docs make no guarantees.
*
* @param application
* @return an initialised Foreground instance
*/
public static Foreground init(Application application){
if (instance == null) {
instance = new Foreground();
application.registerActivityLifecycleCallbacks(instance);
}
return instance;
}
public static Foreground get(Application application){
if (instance == null) {
init(application);
}
return instance;
}
public static Foreground get(Context ctx){
if (instance == null) {
Context appCtx = ctx.getApplicationContext();
if (appCtx instanceof Application) {
init((Application)appCtx);
}
throw new IllegalStateException(
"Foreground is not initialised and " +
"cannot obtain the Application object");
}
return instance;
}
public static Foreground get(){
if (instance == null) {
throw new IllegalStateException(
"Foreground is not initialised - invoke " +
"at least once with parameterised init/get");
}
return instance;
}
public boolean isForeground(){
return foreground;
}
public boolean isBackground(){
return !foreground;
}
public void addListener(Listener listener){
listeners.add(listener);
}
public void removeListener(Listener listener){
listeners.remove(listener);
}
@Override
public void onActivityResumed(Activity activity) {
paused = false;
boolean wasBackground = !foreground;
foreground = true;
if (check != null)
handler.removeCallbacks(check);
if (wasBackground){
Log.i(TAG, "went foreground");
for (Listener l : listeners) {
try {
l.onBecameForeground();
} catch (Exception exc) {
Log.e(TAG, "Listener threw exception!", exc);
}
}
} else {
Log.i(TAG, "still foreground");
}
}
@Override
public void onActivityPaused(Activity activity) {
paused = true;
if (check != null)
handler.removeCallbacks(check);
handler.postDelayed(check = new Runnable(){
@Override
public void run() {
if (foreground && paused) {
foreground = false;
Log.i(TAG, "went background");
for (Listener l : listeners) {
try {
l.onBecameBackground();
} catch (Exception exc) {
Log.e(TAG, "Listener threw exception!", exc);
}
}
} else {
Log.i(TAG, "still foreground");
}
}
}, CHECK_DELAY);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {}
}
@xadh00m

This comment has been minimized.

Copy link

commented Feb 17, 2015

Thanks for this great way to figure out the foreground state of an application!
We have adapted this class (to a BackgroundManager) which has ended up to be even a bit simpler:
https://gist.github.com/xadh00m/1584a58ddb3d724cdd24

@YkmLo

This comment has been minimized.

Copy link

commented Aug 31, 2015

Hi, I got NoClassDefFoundError in line 156. Any idea? It seems the error occurs whenever implementing new Runnable(). My stackoverflow question is http://stackoverflow.com/questions/32303800/noclassdeffounderror-on-implementing-runnable-class-in-android-studio

@j796160836

This comment has been minimized.

Copy link

commented Sep 14, 2015

Hi Steve (@steveliles), I found a bug that I use the Foreground class.
https://gist.github.com/steveliles/11116937#file-foreground-java-L92

public static Foreground get(Context ctx){
    if (instance == null) {
        Context appCtx = ctx.getApplicationContext();
        if (appCtx instanceof Application) {
            init((Application)appCtx);
        }
        throw new IllegalStateException(
            "Foreground is not initialised and " +
            "cannot obtain the Application object");
    }
    return instance;
}

I received the exception when get instances with no init in Application object.
Their will be always throw exception when instances is null.

I think you are lack a return here.

return init((Application)appCtx);
@j796160836

This comment has been minimized.

Copy link

commented Sep 14, 2015

@steveliles I leave this commit here because GitHub gist cannot send pull request.
Please check it out, thanks.

@onlyAnkita

This comment has been minimized.

Copy link

commented Mar 4, 2016

How to implement it in my app

@azorrozua

This comment has been minimized.

Copy link

commented May 4, 2016

Really useful. Thank you!

@susemi99

This comment has been minimized.

Copy link

commented May 13, 2016

@Ankita1405

  1. download that file and add your project.
  2. create Application class see here
  3. insert Foreground.init(this); to onCreate() of Application class
  4. call Foreground.get().isForeground() where you want.
@prgomet

This comment has been minimized.

Copy link

commented Jun 7, 2016

You have to change private boolean foreground = false, paused = true; to private boolean foreground = true, paused = false;,
otherwise this wont work first time after you open app and put it to background.

@vladiulianbogdan

This comment has been minimized.

Copy link

commented Jul 21, 2016

Good job, mate! Thank you!

@saintjab

This comment has been minimized.

Copy link

commented Nov 7, 2016

Good job. Thanks!

@af-fess

This comment has been minimized.

Copy link

commented Nov 8, 2016

Doesn't work for hybrid applications such: Cordova, Unity, Nativescript, react Native

@Iamgiam

This comment has been minimized.

Copy link

commented Feb 1, 2017

Thank you for you hard work. I had a nightmare trying to flag when I went Background to Foreground with crashes on resume with fragments.
This helps massively.

@llew2011

This comment has been minimized.

Copy link

commented Mar 15, 2017

good , thanks

@ecramer

This comment has been minimized.

Copy link

commented Mar 16, 2017

🙌

@nucleons

This comment has been minimized.

Copy link

commented Mar 28, 2017

Fantastic

@Gilb007

This comment has been minimized.

Copy link

commented Mar 31, 2017

thanks a lot

@Kang-heesuk

This comment has been minimized.

Copy link

commented Apr 7, 2017

@steveliles Thanks a lot! I have a question, If I use this source code in my commercial application then, Is there any license like a MIT, Apache something? Plz, let me know :)

@chihung93

This comment has been minimized.

Copy link

commented May 12, 2017

I think we have some issues, When I request Permissions on my device, it's onBecameForeground and onBecameBackground

public class App extends Application implements Foreground.Listener {
    @Override
    public void onCreate(){
        super.onCreate();
        //ForeGround & Background
        Foreground.init(this).addListener(this);
    }
    @Override
    public void onBecameForeground() {
        Log.d("Application","onBecameForeground");
    }

    @Override
    public void onBecameBackground() {
        Log.d("Application","onBecameBackground");
    }
}

@rok5ek

This comment has been minimized.

Copy link

commented Jun 28, 2017

I think you can simplify it like this

public class AppLifecycleListener implements Application.ActivityLifecycleCallbacks {
    private int numStarted;
    private boolean isForeground;

    @Override
    public void onActivityStarted(Activity activity) {
        if (numStarted == 0) {
            isForeground = true;
        }
        numStarted++;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        numStarted--;
        if (numStarted == 0) {
            isForeground = false;
        }
    }
}
@hoangduong95

This comment has been minimized.

Copy link

commented Jul 12, 2017

you saved my day

@xFrenzy47x

This comment has been minimized.

Copy link

commented Oct 6, 2017

Thanks @j796160836 for the solution, I had no idea why it was falling over :)

@hegazy

This comment has been minimized.

Copy link

commented Jan 12, 2018

Forget that and just use ProcessLifecycleOwner from the Android Architecture Components. It's dead simple to use and it also handles when a permission dialog is displayed

class MyApplication : Application(), LifecycleObserver {

    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onAppForegrounded() {
        Log.d(TAG, "In foreground")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onAppBackgrounded() {
        Log.d(TAG, "In background")
    }
}
@CosimoSguanci

This comment has been minimized.

Copy link

commented Jan 30, 2018

It actually works. Great job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.