https://github.com/shiraji/butai
Android library that adds methods isForeground/isBackground/isReturnedFromBackground
https://github.com/shiraji/butai
android-library java kotlin
Last synced: 12 months ago
JSON representation
Android library that adds methods isForeground/isBackground/isReturnedFromBackground
- Host: GitHub
- URL: https://github.com/shiraji/butai
- Owner: shiraji
- License: apache-2.0
- Created: 2017-01-26T13:56:13.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-31T13:57:41.000Z (over 9 years ago)
- Last Synced: 2025-04-07T12:05:35.841Z (about 1 year ago)
- Topics: android-library, java, kotlin
- Language: Java
- Homepage:
- Size: 122 KB
- Stars: 16
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# butai
butai is Android library that adds functions return the application is in foreground, background or return from background.
butai is inspired by [@yshrsmz](https://github.com/yshrsmz)'s [blog entry](http://yslibrary.net/2015/07/30/android_how_to_detect_app_is_background_or_not/) (in Japanese).
The main logic of this library is that butai counts up the number of running Activities using `Application#ActivityLifecycleCallbacks`. Based on the sum of running Activities, `Butai` interface provides that the application is in foreground/background.
# Prerequisite
* minSdk must be higher than 14 (because of `Application#ActivityLifecycleCallbacks`)
* Only one of Java/Kotlin implementation can be used. Not both of them
# How to install & setup?
butai has [Java](library-java) and [Kotlin](library) implementations. Each has different steps to install and setup.
Please read each README.
* [Java](library-java/README.md)
* [Kotlin](library/README.md)
# What the app status can get?
`Butai` interface has following method signatures in kotlin.
```kotlin
fun isReturnedFromBackground(): Boolean
fun isBackground(): Boolean
fun isForeground(): Boolean
```
* `isReturnedFromBackground()` true if the app shows up to foreground, false otherwise. This is true until other Activity launch or kill the Activity.
* `isBackground()` true if the app go to background, false otherwise.
* `isForeground()` true if the app is on foreground, false otherwise.
# How to get the app status?
`MyApplication` class above delegate these methods using Class Delegation.
```kotlin
class MainAktivity : AppCompatActivity() {
override fun onStart() {
super.onStart()
if ((application as MyApplication).isForeground()) Log.d("MainAktivity", "App is foreground")
if ((application as MyApplication).isReturnedFromBackground()) Log.d("MainAktivity", "App returns from background")
}
override fun onStop() {
super.onStop()
if ((application as MyApplication).isForeground()) Log.d("MainAktivity", "App is still foreground")
if ((application as MyApplication).isBackground()) Log.d("MainAktivity", "App goes to background")
}
}
```
# License
```
Copyright 2017 Yoshinori Isogai
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.
```