Last active
January 2, 2024 19:54
-
-
Save pantos27/6faad313ba364c947e2487e80aa25c86 to your computer and use it in GitHub Desktop.
A utility class to keep track if your app is in the foreground or background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
interface StateChangedListener{ | |
fun onForeground() | |
fun onBackground() | |
} | |
var listener: StateChangedListener? = null | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | |
} | |
override fun onActivityStarted(activity: Activity) { | |
started ?: listener?.onForeground() | |
started = activity.hashCode() | |
} | |
override fun onActivityResumed(activity: Activity) { | |
started ?: listener?.onForeground() | |
started = activity.hashCode() | |
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 | |
listener?.onBackground() | |
} | |
} | |
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { | |
} | |
override fun onActivityDestroyed(activity: Activity) { | |
} | |
public fun isAppStarted() = started!=null | |
public fun isAppResumed() = resumed!=null | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pantos27.hamburgersforbreakfast | |
public class MyApplication extends android.app.Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
//register watcher | |
registerActivityLifecycleCallbacks(ApplicationWatcher.INSTANCE); | |
ApplicationWatcher.INSTANCE.setListener(new ApplicationWatcher.StateChangedListener() { | |
@Override | |
public void onForeground() { | |
//you're on! | |
} | |
@Override | |
public void onBackground() { | |
//you're off! | |
} | |
}); | |
} | |
@Override | |
public void onTerminate() { | |
super.onTerminate(); | |
//unregister watcher | |
unregisterActivityLifecycleCallbacks(ApplicationWatcher.INSTANCE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use Kotlin in react native