Skip to content

Commit

Permalink
Switch context ref to lambda ref
Browse files Browse the repository at this point in the history
Sharing an Activity instance through a static property of a receiver is a memory leak, so instead of doing that, this shares a reference to a lambda
  • Loading branch information
MarmadileManteater committed Feb 1, 2024
1 parent 2ef439c commit 536f313
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
@Suppress("DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MediaControlsReceiver.Static.main = this
MediaControlsReceiver.notifyMediaSessionListeners = {
action ->
webView.loadUrl(String.format("javascript: window.notifyMediaSessionListeners('%s')", action))
}
// this keeps android from shutting off the app to conserve battery
keepAliveService = KeepAliveService()
keepAliveIntent = Intent(this, keepAliveService.javaClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ open class MediaControlsReceiver : BroadcastReceiver {
constructor() {
}
companion object Static {
lateinit var main: MainActivity
lateinit var notifyMediaSessionListeners: (String) -> Unit
}


override fun onReceive(context: Context?, intent: Intent?) {
val action = intent!!.action
main.webView.loadUrl(String.format("javascript: window.notifyMediaSessionListeners('%s')", action))
notifyMediaSessionListeners(action!!)
}
}

0 comments on commit 536f313

Please sign in to comment.