RxAppFocus

Additional

Language
Java
Version
0.3.0 (Nov 24, 2017)
Created
Apr 19, 2016
Updated
Nov 24, 2017
Owner
gramboid
Contributor
GrahamBorland
1
Activity
Badge
Generate
Download
Source code
APK file

Advertising

RxAppFocus

A tiny library for Android/RxJava to tell you when your app becomes visible or hidden.

Why would you want to do that?

Maybe you want to trigger a sync with a cloud service when your app is resumed. Maybe you want to stop receiving location updates when your app goes into the background.

RxAppFocus is most useful in a non-Activity context: application startup, receiving broadcasts, responding to asynchronous task completion, etc. (After all, in an Activity, the lifecycle methods already tell you what you need to know.)

Usage

See the rxappfocus-sample module for a working example.

Visibility update stream

In your Application.onCreate():

val focusProvider = AppFocusProvider(this)
focusProvider.getAppFocus().subscribe { visible: Boolean -> /* ... */ }

Quick visibility check

if (focusProvider.isVisible) { /* ... */ }

Visible activity

focusProvider.visibleActivity?.let { activity: Activity -> /* ... */ }

RxJava 1 or 2

The library will provide either an RxJava 1 or 2 Observable, depending on which version of RxJava your app uses. The library is built with compileOnly (i.e. non-transitive) dependencies on both RxJava versions.

RxJava 1

focusProvider.getAppFocus().subscribe { }

RxJava 2

focusProvider.getAppFocus2().subscribe { }

Download

implementation 'com.gramboid:rxappfocus:0.3.1' 

Available on jitpack.io. If you don't already have it, add it to your top-level build.gradle:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

License

Copyright 2016 Gramboid Mobile Systems Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.