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 -> /* ... */
}
Download
compile 'com.gramboid.rxappfocus:rxappfocus:0.2.2'
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.