diff --git a/link/api/link.api b/link/api/link.api index b80171d0e11..93155a13182 100644 --- a/link/api/link.api +++ b/link/api/link.api @@ -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 ()V public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/link/LinkActivityResult$Canceled; diff --git a/link/src/main/java/com/stripe/android/link/LinkActivity.kt b/link/src/main/java/com/stripe/android/link/LinkActivity.kt index 5ece38b0e04..4d508fea200 100644 --- a/link/src/main/java/com/stripe/android/link/LinkActivity.kt +++ b/link/src/main/java/com/stripe/android/link/LinkActivity.kt @@ -76,4 +76,9 @@ internal class LinkActivity : AppCompatActivity() { ) this@LinkActivity.finish() } + + override fun onDestroy() { + super.onDestroy() + viewModel.unregisterActivity() + } } diff --git a/link/src/main/java/com/stripe/android/link/LinkActivityContract.kt b/link/src/main/java/com/stripe/android/link/LinkActivityContract.kt index df495019a90..3b86fd147d0 100644 --- a/link/src/main/java/com/stripe/android/link/LinkActivityContract.kt +++ b/link/src/main/java/com/stripe/android/link/LinkActivityContract.kt @@ -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" } } diff --git a/link/src/main/java/com/stripe/android/link/LinkActivityViewModel.kt b/link/src/main/java/com/stripe/android/link/LinkActivityViewModel.kt index a978c16ca2c..7f234d312ed 100644 --- a/link/src/main/java/com/stripe/android/link/LinkActivityViewModel.kt +++ b/link/src/main/java/com/stripe/android/link/LinkActivityViewModel.kt @@ -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) { @@ -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 { diff --git a/link/src/test/java/com/stripe/android/link/LinkActivityViewModelTest.kt b/link/src/test/java/com/stripe/android/link/LinkActivityViewModelTest.kt index 057bc9c1b6b..78f9be31d53 100644 --- a/link/src/test/java/com/stripe/android/link/LinkActivityViewModelTest.kt +++ b/link/src/test/java/com/stripe/android/link/LinkActivityViewModelTest.kt @@ -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 @@ -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) + } }