Created
September 3, 2015 14:04
-
-
Save evant/b6bdbde7dd5fa432a351 to your computer and use it in GitHub Desktop.
Check when app is in forground/background (api 14+)
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
public class AppBackgroundChecker implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 { | |
private boolean isBackground; | |
public void register(Application application) { | |
application.registerActivityLifecycleCallbacks(this); | |
application.registerComponentCallbacks(this); | |
} | |
@Override | |
public void onTrimMemory(int level) { | |
if (level == TRIM_MEMORY_UI_HIDDEN) { | |
isBackground = true; | |
// Do anything for background | |
} | |
} | |
@Override | |
public void onConfigurationChanged(Configuration configuration) { | |
} | |
@Override | |
public void onLowMemory() { | |
} | |
@Override | |
public void onActivityCreated(Activity activity, Bundle bundle) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
} | |
@Override | |
public void onActivityResumed(Activity activity) { | |
if (isBackground) { | |
isBackground = false; | |
// Do what you need for foreground | |
} | |
} | |
@Override | |
public void onActivityPaused(Activity activity) { | |
} | |
@Override | |
public void onActivityStopped(Activity activity) { | |
} | |
@Override | |
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { | |
} | |
@Override | |
public void onActivityDestroyed(Activity activity) { | |
} | |
} |
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
public class MainApp extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
new AppBackgroundChecker().register(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment