Created
July 20, 2018 05:20
-
-
Save NeerajMoudgil/cdea451f7bc79d33d1a1513eb1e2031d to your computer and use it in GitHub Desktop.
application class to check android app is in foreground or background
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
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