Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.thim:RxAppState:4.0.1'
}
dependencies {
implementation("com.github.thim:RxAppState:4.0.1")
}
<dependency>
<groupId>com.github.thim</groupId>
<artifactId>RxAppState</artifactId>
<version>4.0.1</version>
</dependency>
libraryDependencies += "com.github.thim" % "RxAppState" % "4.0.1"
:dependencies [[com.github.thim/RxAppState "4.0.1"]]
A simple, reactive Android library based on RxJava that monitors app state changes.
It notifies subscribers every time the app goes into background and comes back into foreground.
A typical use case is, for example, session tracking for analytics purposes or suppressing push notifications when the app is currently visible to the user.
Android has this ancient pain of not providing any type of callback to know if your app is currently in the foreground or background.
It is lacking an equivalent of the iOS UIApplicationDelegate
which offers callbacks like applicationDidEnterBackground
and applicationDidBecomeActive
.
There are two popular discussions on this topic on StackOverflow:
This library internally uses a combination of ActivityLifecycleCallbacks
and the onTrimMemory(int level)
callback to identify the current app state.
Just check out the source code (mainly: AppStateRecognizer).
The implementation is dead simple.
You most probably want to monitor for app state changes in your application's onCreate()
method
in which case you also don't need to worry about unsubscribing from the Observable
.
Remember that if you subscribe in an Activity
or Fragment
don't forget to unsubscribe to avoid memory leaks.
RxAppStateMonitor.monitor(this).subscribe(new Action1<AppState>() {
@Override
public void call(AppState appState) {
switch (appState) {
case FOREGROUND:
// Hocus Pocus...
break;
case BACKGROUND:
// Abracadabra!
break;
}
}
});
If you haven't jumped onto the hip RX bandwagon yet, you can also register oldskool callback listeners:
AppStateMonitor appStateMonitor = RxAppStateMonitor.create(this);
appStateMonitor.addListener(new AppStateListener() {
@Override
public void onAppDidEnterForeground() {
// ...
}
@Override
public void onAppDidEnterBackground() {
// ...
}
});
appStateMonitor.start();
Check out the sample project for an example implementation.
This library is published via JitPack.
Step 1. Add the JitPack repository to your project root build.gradle
:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency to your app build.gradle
:
dependencies {
compile 'com.jenzz:RxAppState:2.0.1'
}
This project is licensed under the MIT License.