Skip to content

Instantly share code, notes, and snippets.

@AlonsoFloo
Created November 17, 2018 16:58
Show Gist options
  • Save AlonsoFloo/6651a55266079ff8392fd6f825669cdb to your computer and use it in GitHub Desktop.
Save AlonsoFloo/6651a55266079ff8392fd6f825669cdb to your computer and use it in GitHub Desktop.
Android foreground and background listener and manager
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initComponents();
}
private void initComponents() {
BackgroundManager.init(this);
BackgroundManager.get().registerListener(new BackgroundManager.Listener() {
@Override
public void onBecameForeground() {
}
@Override
public void onBecameBackground() {
}
});
}
}
public class BackgroundManager implements Application.ActivityLifecycleCallbacks {
public static final long BACKGROUND_DELAY = 500;
private static BackgroundManager sInstance;
public interface Listener {
void onBecameForeground();
void onBecameBackground();
}
private boolean mInBackground = true;
private final List<Listener> listeners = new ArrayList<>();
private final Handler mBackgroundDelayHandler = new Handler();
private Runnable mBackgroundTransition;
public static BackgroundManager init(Application application) {
if (sInstance == null) {
sInstance = new BackgroundManager(application);
}
return sInstance;
}
public static BackgroundManager get() {
if (sInstance == null) {
throw new RuntimeException("Use BackgroundManager.init before");
}
return sInstance;
}
private BackgroundManager(Application application) {
application.registerActivityLifecycleCallbacks(this);
}
public void registerListener(Listener listener) {
if (!listeners.contains(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();
}
}
private void notifyOnBecameForeground() {
for (Listener listener : listeners) {
try {
listener.onBecameForeground();
} catch (Exception e) {
}
}
}
@Override
public void onActivityPaused(Activity activity) {
if (!mInBackground && mBackgroundTransition == null) {
mBackgroundTransition = new Runnable() {
@Override
public void run() {
mInBackground = true;
mBackgroundTransition = null;
notifyOnBecameBackground();
}
};
mBackgroundDelayHandler.postDelayed(mBackgroundTransition, BACKGROUND_DELAY);
}
}
private void notifyOnBecameBackground() {
for (Listener listener : listeners) {
try {
listener.onBecameBackground();
} catch (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