Skip to content

Commit

Permalink
Fix potential memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Sep 27, 2024
1 parent ee4d8fb commit 79c1e42
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions link/api/link.api
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public final class com/stripe/android/link/ComposableSingletons$LinkActivityKt {
public final fun getLambda-5$link_release ()Lkotlin/jvm/functions/Function4;
}

public final class com/stripe/android/link/LinkActivityContract$Companion {
}

public final class com/stripe/android/link/LinkActivityResult$Canceled$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/link/LinkActivityResult$Canceled;
Expand Down
5 changes: 5 additions & 0 deletions link/src/main/java/com/stripe/android/link/LinkActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ internal class LinkActivity : AppCompatActivity() {
)
this@LinkActivity.finish()
}

override fun onDestroy() {
super.onDestroy()
viewModel.unregisterActivity()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LinkActivityContract @Inject internal constructor(
)

companion object {
const val EXTRA_RESULT =
internal const val EXTRA_RESULT =
"com.stripe.android.link.LinkActivityContract.extra_result"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavHostController

internal class LinkActivityViewModel : ViewModel() {
lateinit var navController: NavHostController
lateinit var dismissWithResult: (LinkActivityResult) -> Unit
var navController: NavHostController? = null
var dismissWithResult: ((LinkActivityResult) -> Unit)? = null

fun handleViewAction(action: LinkAction) {
when (action) {
Expand All @@ -15,7 +15,12 @@ internal class LinkActivityViewModel : ViewModel() {
}

private fun handleBackPressed() {
dismissWithResult(LinkActivityResult.Canceled())
dismissWithResult?.invoke(LinkActivityResult.Canceled())
}

fun unregisterActivity() {
navController = null
dismissWithResult = null
}

internal class Factory : ViewModelProvider.Factory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.link

import androidx.navigation.NavHostController
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
Expand Down Expand Up @@ -39,4 +40,12 @@ internal class LinkActivityViewModelTest {

verify(dismissWithResult).invoke(LinkActivityResult.Canceled())
}

@Test
fun `test that activity unregister removes dismissWithResult and nav controller`() = runTest(dispatcher) {
vm.unregisterActivity()

assertThat(vm.dismissWithResult).isEqualTo(null)
assertThat(vm.navController).isEqualTo(null)
}
}

0 comments on commit 79c1e42

Please sign in to comment.