Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package com.pantos27.hamburgersforbreakfast
  2.  
  3. import android.app.Activity
  4. import android.os.Bundle
  5. /**
  6. * A utility class to keep track if your app is in the foreground or background
  7. * without any special permission or API restrictions
  8. * Note that if your app has any activities that run on a different
  9. * process (through the process attribute in your manifest) this utility might not be persistent
  10. *
  11. */
  12. object ApplicationWatcher : android.app.Application.ActivityLifecycleCallbacks {
  13. private var started : Int? = null
  14. private var resumed : Int? = null
  15.  
  16. override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
  17. }
  18.  
  19. override fun onActivityStarted(activity: Activity) {
  20. started = activity.hashCode()
  21. }
  22.  
  23. override fun onActivityResumed(activity: Activity) {
  24. resumed = activity.hashCode()
  25. }
  26.  
  27. override fun onActivityPaused(activity: Activity) {
  28. if (resumed==activity.hashCode()){
  29. resumed=null
  30. }
  31. }
  32.  
  33. override fun onActivityStopped(activity: Activity) {
  34. if (started==activity.hashCode()){
  35. started=null
  36. }
  37. }
  38.  
  39. override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {
  40. }
  41.  
  42. override fun onActivityDestroyed(activity: Activity) {
  43. }
  44.  
  45. public fun isAppStarted() = started!=null
  46. public fun isAppResumed() = resumed!=null
  47. }
Add Comment
Please, Sign In to add comment