Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pantos27.hamburgersforbreakfast
- import android.app.Activity
- import android.os.Bundle
- /**
- * A utility class to keep track if your app is in the foreground or background
- * without any special permission or API restrictions
- * Note that if your app has any activities that run on a different
- * process (through the process attribute in your manifest) this utility might not be persistent
- *
- */
- object ApplicationWatcher : android.app.Application.ActivityLifecycleCallbacks {
- private var started : Int? = null
- private var resumed : Int? = null
- override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
- }
- override fun onActivityStarted(activity: Activity) {
- started = activity.hashCode()
- }
- override fun onActivityResumed(activity: Activity) {
- resumed = activity.hashCode()
- }
- override fun onActivityPaused(activity: Activity) {
- if (resumed==activity.hashCode()){
- resumed=null
- }
- }
- override fun onActivityStopped(activity: Activity) {
- if (started==activity.hashCode()){
- started=null
- }
- }
- override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {
- }
- override fun onActivityDestroyed(activity: Activity) {
- }
- public fun isAppStarted() = started!=null
- public fun isAppResumed() = resumed!=null
- }
Add Comment
Please, Sign In to add comment