Skip to content

Instantly share code, notes, and snippets.

@jraska
Last active March 18, 2020 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jraska/d0c23d6f64990b85ef7b46aba35a17aa to your computer and use it in GitHub Desktop.
Save jraska/d0c23d6f64990b85ef7b46aba35a17aa to your computer and use it in GitHub Desktop.
class TopActivityProvider {
private val pendingActions = mutableListOf<(Activity) -> Unit>()
var topActivity: Activity? = null
private set
@AnyThread
fun onTopActivity(action: (Activity) -> Unit) {
val topActivity = this.topActivity
if (topActivity == null) {
addPendingAction(action)
} else {
if (isMainThread()) {
action(topActivity)
} else {
addPendingAction(action)
topActivity.runOnUiThread { executePendingActions() }
}
}
}
@MainThread
private fun executePendingActions() { ... }
@AnyThread
private fun addPendingAction(action: (Activity) -> Unit) { ... }
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment