Skip to content

Instantly share code, notes, and snippets.

@NeerajMoudgil
Created July 20, 2018 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeerajMoudgil/cdea451f7bc79d33d1a1513eb1e2031d to your computer and use it in GitHub Desktop.
Save NeerajMoudgil/cdea451f7bc79d33d1a1513eb1e2031d to your computer and use it in GitHub Desktop.
application class to check android app is in foreground or background
public class MyApplication extends Application implements LifecycleObserver
{
private static Context appContext;
public static boolean wasInBackground;
@Override
public void onCreate()
{
super.onCreate();
appContext=this;
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
public static Context getAppContext() {
return appContext;
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
// app moved to foreground
wasInBackground=true;
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onMoveToBackground() {
// app moved to background
wasInBackground =false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment