Skip to content

Instantly share code, notes, and snippets.

@chihung93
Forked from xadh00m/BackgroundManager.java
Created April 24, 2017 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chihung93/3df0dc25a9d4b3817be1a676a233f755 to your computer and use it in GitHub Desktop.
Save chihung93/3df0dc25a9d4b3817be1a676a233f755 to your computer and use it in GitHub Desktop.
BackgroundManager
package com.android.utils;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
// based on https://gist.github.com/steveliles/11116937
public class BackgroundManager implements Application.ActivityLifecycleCallbacks {
private static final Logger LOG = Logger.getLogger(BackgroundManager.class);
public static final long BACKGROUND_DELAY = 500;
private static BackgroundManager sInstance;
public interface Listener {
public void onBecameForeground();
public void onBecameBackground();
}
private boolean mInBackground = true;
private final List<Listener> listeners = new ArrayList<Listener>();
private final Handler mBackgroundDelayHandler = new Handler();
private Runnable mBackgroundTransition;
public static BackgroundManager get() {
if (sInstance == null) {
sInstance = new BackgroundManager();
}
return sInstance;
}
private BackgroundManager(Application application) {
application.registerActivityLifecycleCallbacks(this);
}
public void registerListener(Listener listener) {
listeners.add(listener);
}
public void unregisterListener(Listener listener) {
listeners.remove(listener);
}
public boolean isInBackground() {
return mInBackground;
}
@Override
public void onActivityResumed(Activity activity) {
if (mBackgroundTransition != null) {
mBackgroundDelayHandler.removeCallbacks(mBackgroundTransition);
mBackgroundTransition = null;
}
if (mInBackground) {
mInBackground = false;
notifyOnBecameForeground();
LOG.info("Application went to foreground");
}
}
private void notifyOnBecameForeground() {
for (Listener listener : listeners) {
try {
listener.onBecameForeground();
} catch (Exception e) {
LOG.error("Listener threw exception!", e);
}
}
}
@Override
public void onActivityPaused(Activity activity) {
if (!mInBackground && mBackgroundTransition == null) {
mBackgroundTransition = new Runnable() {
@Override
public void run() {
mInBackground = true;
mBackgroundTransition = null;
notifyOnBecameBackground();
LOG.info("Application went to background");
}
};
mBackgroundDelayHandler.postDelayed(mBackgroundTransition, BACKGROUND_DELAY);
}
}
private void notifyOnBecameBackground() {
for (Listener listener : listeners) {
try {
listener.onBecameBackground();
} catch (Exception e) {
LOG.error("Listener threw exception!", e);
}
}
}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment