百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

RxAppState

> 编程语言
开源

[已弃用] 一个简单的 Android 库,用于监控应用程序状态变化(后台/前台)。

193 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

[已弃用] 一个简单的 Android 库,用于监控应用程序状态变化(后台/前台)。

DEPRECATED

RxAppState is deprecated. No more development will be taking place.

Use Google's ProcessLifecycleOwner instead which is part of Android Architecture Components.

An implementation can be as simple as:

java
public class AppLifecycleListener implements LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppDidEnterForeground() {
        // ...
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppDidEnterBackground() {
       // ...
    }
}

Register the observer like this:

java
ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleListener());

Here is a great blog post that explains it in more detail: Detecting when an Android app backgrounds in 2018

RxAppState

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.

Background

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:

  • How to detect when an Android app goes to the background and come back to the foreground
  • Checking if an Android application is running in the background

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: DefaultAppStateRecognizer). The implementation is dead simple.

Usage

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 unregistering your AppStateListener. Remember that if you subscribe in an Activity or a Fragment, don't forget to unsubscribe to avoid memory leaks.

java
AppStateMonitor appStateMonitor = RxAppStateMonitor.create(this);
appStateMonitor.addListener(new AppStateListener() {
    @Override
    public void onAppDidEnterForeground() {
        // ...
    }

    @Override
    public void onAppDidEnterBackground() {
        // ...
    }
});
appStateMonitor.start();

Example

Check out the sample project for an example implementation.

Download

Grab it via Gradle:

groovy
dependencies {
  compile 'com.jenzz.appstate:appstate:3.0.1'
}

Note: There are adapters available for RxJava and RxJava2.

License

This project is licensed under the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言