Skip to content

Commit f95ab59

Browse files
authored
Fix: Prevent memory leak from implicit SAM conversion (#588)
The lint tool reported an "ImplicitSamInstance" warning in PictureInPictureSnippets.kt. Lambdas used as listeners were being implicitly converted to new SAM instances, causing removeOnUserLeaveHintListener to fail because the listener instances did not match. This could lead to a memory leak. This commit fixes the issue by explicitly wrapping the listener lambdas in a 'Runnable' instance. This ensures a stable object reference is used for both adding and removing the listener.
1 parent e2de07a commit f95ab59

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ fun PipListenerPreAPI12(shouldEnterPipMode: Boolean) {
356356
) {
357357
val context = LocalContext.current
358358
DisposableEffect(context) {
359-
val onUserLeaveBehavior: () -> Unit = {
359+
val onUserLeaveBehavior = Runnable {
360360
context.findActivity()
361361
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())
362362
}
@@ -384,7 +384,7 @@ fun EnterPiPPre12(shouldEnterPipMode: Boolean) {
384384
) {
385385
val context = LocalContext.current
386386
DisposableEffect(context) {
387-
val onUserLeaveBehavior: () -> Unit = {
387+
val onUserLeaveBehavior = Runnable {
388388
if (currentShouldEnterPipMode) {
389389
context.findActivity()
390390
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())

0 commit comments

Comments
 (0)