Skip to content

Instantly share code, notes, and snippets.

@bangarharshit
Last active December 6, 2015 04:04
Show Gist options
  • Select an option

  • Save bangarharshit/34196f52cc86b6023758 to your computer and use it in GitHub Desktop.

Select an option

Save bangarharshit/34196f52cc86b6023758 to your computer and use it in GitHub Desktop.
public class ApplicationLifeCycleHandler implements Application.ActivityLifecycleCallbacks {
// Using the approach defined here - http://steveliles.github.io/is_my_android_app_currently_foreground_or_background.html
public interface Listener {
public void onBecameForeground();
public void onBecameBackground();
}
public class ClienStateIndicationListener implements Listener {
XMPPConnection xmppConnection;
public ClienStateIndicationListener(XMPPConnection xmppConnection) {
this.xmppConnection = xmppConnection;
}
public void onBecameForeground() {
ClientStateIndicationManager.active(xmppConnection);
}
public void onBecameBackground() {
ClientStateIndicationManager.inactive(xmppConnection);
}
}
public static final long CHECK_DELAY = 500;
public static final String TAG = ApplicationLifeCycleHandler.class.getName();
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;
private boolean foreground = false, isPaused = true;
public void addListener(Listener listener){
listeners.add(listener);
}
public void removeListener(Listener listener){
listeners.remove(listener);
}
public ApplicationLifeCycleHandler() {
super();
// Resetting since static variables are not clear for forceful app exit.
started = 0;
resumed = 0;
paused = 0;
stopped = 0;
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
started++;
}
@Override
public void onActivityResumed(Activity activity) {
resumed++;
isPaused = 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++;
if (check != null)
handler.removeCallbacks(check);
handler.postDelayed(check = new Runnable(){
@Override
public void run() {
if (foreground && isPaused) {
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 onActivityStopped(Activity activity) {
stopped++;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
// If you want a static function you can use to check if your application is
// foreground/background, you can use the following:
// Replace the four variables above with these four
private int resumed;
private int paused;
private int started;
private int stopped;
// And these two public static functions
public boolean isApplicationVisible() {
return started > stopped;
}
public boolean isApplicationInForeground() {
return resumed > paused;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment