Last active
September 23, 2015 06:25
-
-
Save shivam340/f99ff18c4251601f0b00 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.os.Bundle; | |
import java.util.HashMap; | |
import timber.log.Timber; | |
/** | |
* Check if the application is in the foreground or background. | |
* * | |
* Register this callbacks for an application | |
* Application application = (Application) context.getApplicationContext(); | |
* application.registerActivityLifecycleCallbacks(new BaseLifeCycleCallbacks()); | |
* * | |
* Note: These callbacks can be registered at any level of the application lifecycle. | |
* Previous methods to get the application lifecycle forced the lifecycle callbacks to be registered | |
* at the start of the application in a dedicated Application class. | |
*/ | |
public class BaseLifeCycleCallbacks implements Application.ActivityLifecycleCallbacks { | |
private static final String TAG = BaseLifeCycleCallbacks.class.getSimpleName(); | |
private HashMap<String, Integer> mActivities = null; | |
public BaseLifeCycleCallbacks() { | |
mActivities = new HashMap<String, Integer>(); | |
} | |
@Override | |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
//map Activity unique class name with 1 on foreground | |
mActivities.put(activity.getLocalClassName(), 1); | |
applicationStatus(); | |
} | |
@Override | |
public void onActivityResumed(Activity activity) { | |
} | |
@Override | |
public void onActivityPaused(Activity activity) { | |
} | |
@Override | |
public void onActivityStopped(Activity activity) { | |
//map Activity unique class name with 0 on foreground | |
mActivities.put(activity.getLocalClassName(), 0); | |
applicationStatus(); | |
} | |
@Override | |
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | |
} | |
@Override | |
public void onActivityDestroyed(Activity activity) { | |
} | |
/** | |
* Check if any activity is in the foreground | |
*/ | |
private boolean isBackGround() { | |
for (String s : mActivities.keySet()) { | |
if (mActivities.get(s) == 1) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Returns application status. | |
* @return false if application is in background mode else return true. | |
*/ | |
public boolean applicationStatus() { | |
try { | |
boolean isInBackground = isBackGround(); | |
Timber.d(TAG + " %s ", "Is application running in background : " + isInBackground); | |
return isInBackground; | |
}catch (NullPointerException ex){ | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment