Created
January 30, 2016 09:17
-
-
Save Ronelg/6e5d47b7cdf92b37de88 to your computer and use it in GitHub Desktop.
Android application foreground or bakcground
This file contains hidden or 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
/** | |
* 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 AppLifeCycle implements Application.ActivityLifecycleCallbacks { | |
HashMap<String, Integer> activities; | |
public AppLifeCycle() { | |
activities = new HashMap<>(); | |
} | |
@Override | |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
//map Activity unique class name with 1 on foreground | |
activities.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 | |
activities.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 : activities.keySet()) { | |
if (activities.get(s) == 1) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Log application status. | |
*/ | |
private void applicationStatus() { | |
Log.d("ApplicationStatus", "Is application background" + isBackGround()); | |
if (isBackGround()) { | |
//Do something if the application is in background | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment