Skip to content

Instantly share code, notes, and snippets.

@savasadar
Last active May 17, 2019 13:36
Show Gist options
  • Save savasadar/39a8234ca7d4bdb59075d029ff16ebf7 to your computer and use it in GitHub Desktop.
Save savasadar/39a8234ca7d4bdb59075d029ff16ebf7 to your computer and use it in GitHub Desktop.
Detect android app foreground and background state with LifecycleObserver
import android.app.Application;
import android.util.Log;
import android.arch.lifecycle.*;
public class App extends Application implements LifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void onAppBackgrounded() {
Log.d("AppLog", "App in background");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void onAppForegrounded() {
Log.d("AppLog", "App in foreground");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment