Last active
May 17, 2019 13:36
-
-
Save savasadar/39a8234ca7d4bdb59075d029ff16ebf7 to your computer and use it in GitHub Desktop.
Detect android app foreground and background state with LifecycleObserver
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
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