Created
September 1, 2015 21:09
-
-
Save technoir42/1688d99c26b6f2235800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.Application; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Detects when the current application enters background or foreground and notifies its listeners. | |
*/ | |
public class BackgroundTransitionMonitor { | |
public interface Listener { | |
void onEnterBackground(); | |
void onEnterForeground(); | |
} | |
private static final String TAG = "BackgroundTransitionMonitor"; | |
private static final long BACKGROUND_TRANSITION_DELAY = 500; | |
private static BackgroundTransitionMonitor instance; | |
private final Application application; | |
private final Handler backgroundCheckHandler = new Handler(); | |
private final List<Listener> listeners = new ArrayList<>(); | |
private boolean isBackground = false; | |
public static synchronized BackgroundTransitionMonitor getInstance(Context context) { | |
if (instance == null) { | |
instance = new BackgroundTransitionMonitor((Application) context.getApplicationContext()); | |
} | |
return instance; | |
} | |
private BackgroundTransitionMonitor(Application application) { | |
this.application = application; | |
} | |
public void addListener(Listener listener) { | |
if (listeners.isEmpty()) { | |
application.registerActivityLifecycleCallbacks(lifecycleCallbacks); | |
} | |
listeners.add(listener); | |
} | |
public void removeListener(Listener listener) { | |
listeners.remove(listener); | |
if (listeners.isEmpty()) { | |
application.unregisterActivityLifecycleCallbacks(lifecycleCallbacks); | |
} | |
} | |
private final Application.ActivityLifecycleCallbacks lifecycleCallbacks = new Application.ActivityLifecycleCallbacks() { | |
@Override | |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
} | |
@Override | |
public void onActivityResumed(Activity activity) { | |
backgroundCheckHandler.removeCallbacks(backgroundTransitionRunnable); | |
if (isBackground) { | |
isBackground = false; | |
for (Listener listener : listeners) { | |
listener.onEnterForeground(); | |
} | |
} | |
} | |
@Override | |
public void onActivityPaused(Activity activity) { | |
if (!isBackground) { | |
backgroundCheckHandler.removeCallbacks(backgroundTransitionRunnable); | |
backgroundCheckHandler.postDelayed(backgroundTransitionRunnable, BACKGROUND_TRANSITION_DELAY); | |
} | |
} | |
@Override | |
public void onActivityStopped(Activity activity) { | |
} | |
@Override | |
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | |
} | |
@Override | |
public void onActivityDestroyed(Activity activity) { | |
} | |
}; | |
private final Runnable backgroundTransitionRunnable = new Runnable() { | |
@Override | |
public void run() { | |
isBackground = true; | |
for (Listener listener : listeners) { | |
listener.onEnterBackground(); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment