• This repository has been archived on 16/Jan/2019
  • Stars
    star
    195
  • Rank 198,069 (Top 4 %)
  • Language
    Java
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[DEPRECATED] A simple Android library that monitors app state changes (background / foreground).

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:

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:

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 Build Status

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:

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.

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:

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.

More Repositories

1

Android-UndoBar

[DEPRECATED] An implementation of Android's Undo Bar as seen in Google's Gmail app.
Java
582
star
2

Android-MaterialPreference

[DEPRECATED] A simple backward-compatible implementation of a Material Design Preference aka settings item
Java
509
star
3

ContentProviderHelper

A nice little Android app that helps developers to discover and query content providers.
Java
112
star
4

Android-MultiStateListView

A simple Android ListView that lets you define three states: Loading, Empty & Error
Java
72
star
5

Android-StaticLauncher

An example project of Android annotation processing using Gradle.
Java
38
star
6

Java-PojoBuilder

A Java code generator for the builder pattern using annotation processing
Java
35
star
7

gradle-android-buildconstants-plugin

A Gradle plugin for Android which generates both Java and XML constants as part of the build process.
Groovy
28
star
8

Java-NoOp

Easy no-op Java interface implementations
Java
21
star
9

social-web-search

Search the social web... Find people & posts on Twitter, Facebook and Google+.
13
star
10

.shell_config

A collection of all the stuff I'm using to customize my cmd prompt.
Shell
3
star
11

java-android-utils

A collection of useful utilities, templates and snippets for Java & Android development that I find myself using in many projects.
Java
2
star
12

Android-DemoApp

A simple demo app for an offline-first architecture.
Kotlin
2
star
13

leo-chrome

A simple Chrome extension for the LEO dictionary (http://goo.gl/7eMdg).
JavaScript
1
star
14

RxJava-Tidbits

Tips & Tricks and solutions to common, real-world problems using RxJava
Java
1
star
15

Android-Sandbox

My little sandbox for trying out cool Android stuff.
Java
1
star
16

Android-PeopleNotes

Android app to keep notes about people
Kotlin
1
star
17

TheTVDB-Retrofit

Java API for TheTVDB.com based on Square's Retrofit REST client
Java
1
star