diff --git a/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetActivity.kt b/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetActivity.kt index e33a8f5d206..5be53db1dd1 100644 --- a/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetActivity.kt +++ b/financial-connections/src/main/java/com/stripe/android/financialconnections/FinancialConnectionsSheetActivity.kt @@ -3,6 +3,7 @@ package com.stripe.android.financialconnections import android.content.Intent import android.net.Uri import android.os.Bundle +import androidx.activity.addCallback import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult import androidx.appcompat.app.AppCompatActivity import com.airbnb.mvrx.Mavericks @@ -42,6 +43,10 @@ internal class FinancialConnectionsSheetActivity : viewModel.onEach { postInvalidate() } if (savedInstanceState != null) viewModel.onActivityRecreated() } + + onBackPressedDispatcher.addCallback { + finishWithResult(FinancialConnectionsSheetActivityResult.Canceled) + } } override fun onResume() { @@ -49,11 +54,6 @@ internal class FinancialConnectionsSheetActivity : viewModel.onResume() } - override fun onBackPressed() { - super.onBackPressed() - finishWithResult(FinancialConnectionsSheetActivityResult.Canceled) - } - /** * Handles new intents in the form of the redirect from the custom tab hosted auth flow */ diff --git a/payments-core/src/main/java/com/stripe/android/view/PaymentAuthWebViewActivity.kt b/payments-core/src/main/java/com/stripe/android/view/PaymentAuthWebViewActivity.kt index e4251379851..32c49bf6ec0 100644 --- a/payments-core/src/main/java/com/stripe/android/view/PaymentAuthWebViewActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/view/PaymentAuthWebViewActivity.kt @@ -6,6 +6,7 @@ import android.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.addCallback import androidx.activity.viewModels import androidx.annotation.ColorInt import androidx.annotation.VisibleForTesting @@ -59,6 +60,14 @@ class PaymentAuthWebViewActivity : AppCompatActivity() { setSupportActionBar(viewBinding.toolbar) customizeToolbar() + onBackPressedDispatcher.addCallback { + if (viewBinding.webView.canGoBack()) { + viewBinding.webView.goBack() + } else { + cancelIntentSource() + } + } + val clientSecret = args.clientSecret setResult(Activity.RESULT_OK, createResultIntent(viewModel.paymentResult)) @@ -148,14 +157,6 @@ class PaymentAuthWebViewActivity : AppCompatActivity() { return super.onOptionsItemSelected(item) } - override fun onBackPressed() { - if (viewBinding.webView.canGoBack()) { - viewBinding.webView.goBack() - } else { - cancelIntentSource() - } - } - private fun cancelIntentSource() { setResult(Activity.RESULT_OK, viewModel.cancellationResult) finish() diff --git a/payments-core/src/main/java/com/stripe/android/view/PaymentFlowActivity.kt b/payments-core/src/main/java/com/stripe/android/view/PaymentFlowActivity.kt index a0f9f564535..8a3f2685e0d 100644 --- a/payments-core/src/main/java/com/stripe/android/view/PaymentFlowActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/view/PaymentFlowActivity.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Intent import android.os.Bundle import android.view.ViewGroup +import androidx.activity.addCallback import androidx.activity.viewModels import androidx.viewpager.widget.ViewPager import com.stripe.android.CustomerSession @@ -78,6 +79,11 @@ class PaymentFlowActivity : StripeActivity() { paymentFlowPagerAdapter.shippingInformation = shippingInformation paymentFlowPagerAdapter.selectedShippingMethod = viewModel.selectedShippingMethod + val onBackPressedCallback = onBackPressedDispatcher.addCallback { + viewModel.currentPage -= 1 + viewPager.currentItem = viewModel.currentPage + } + viewPager.adapter = paymentFlowPagerAdapter viewPager.addOnPageChangeListener( object : ViewPager.OnPageChangeListener { @@ -89,6 +95,8 @@ class PaymentFlowActivity : StripeActivity() { viewModel.isShippingInfoSubmitted = false paymentFlowPagerAdapter.isShippingInfoSubmitted = false } + + onBackPressedCallback.isEnabled = hasPreviousPage() } override fun onPageScrollStateChanged(i: Int) { @@ -246,15 +254,6 @@ class PaymentFlowActivity : StripeActivity() { finish() } - override fun onBackPressed() { - if (hasPreviousPage()) { - viewModel.currentPage -= 1 - viewPager.currentItem = viewModel.currentPage - } else { - super.onBackPressed() - } - } - internal companion object { internal const val PRODUCT_TOKEN: String = "PaymentFlowActivity" } diff --git a/payments-core/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt b/payments-core/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt index a3949642889..6c2a8dd73be 100644 --- a/payments-core/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt @@ -9,6 +9,7 @@ import android.text.util.Linkify import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.activity.addCallback import androidx.activity.viewModels import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AppCompatActivity @@ -100,6 +101,10 @@ class PaymentMethodsActivity : AppCompatActivity() { window.addFlags(it) } + onBackPressedDispatcher.addCallback { + finishWithResult(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED) + } + viewModel.snackbarData.observe(this) { snackbarText -> snackbarText?.let { Snackbar.make(viewBinding.coordinator, it, Snackbar.LENGTH_SHORT).show() @@ -213,10 +218,6 @@ class PaymentMethodsActivity : AppCompatActivity() { } } - override fun onBackPressed() { - finishWithResult(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED) - } - private fun fetchCustomerPaymentMethods() { viewModel.getPaymentMethods().observe(this) { result -> result.fold( diff --git a/payments-core/src/main/java/com/stripe/android/view/StripeActivity.kt b/payments-core/src/main/java/com/stripe/android/view/StripeActivity.kt index 29afa80b018..606a037e2fc 100644 --- a/payments-core/src/main/java/com/stripe/android/view/StripeActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/view/StripeActivity.kt @@ -66,7 +66,7 @@ abstract class StripeActivity : AppCompatActivity() { } else { val handled = super.onOptionsItemSelected(item) if (!handled) { - onBackPressed() + onBackPressedDispatcher.onBackPressed() } handled } diff --git a/payments-core/src/test/java/com/stripe/android/view/PaymentMethodsActivityTest.kt b/payments-core/src/test/java/com/stripe/android/view/PaymentMethodsActivityTest.kt index cfbcd0e6166..9c839f3b844 100644 --- a/payments-core/src/test/java/com/stripe/android/view/PaymentMethodsActivityTest.kt +++ b/payments-core/src/test/java/com/stripe/android/view/PaymentMethodsActivityTest.kt @@ -239,7 +239,7 @@ class PaymentMethodsActivityTest { paymentMethodsAdapter.selectedPaymentMethodId = PaymentMethodFixtures.CARD_PAYMENT_METHODS[0].id - activity.onBackPressed() + activity.onBackPressedDispatcher.onBackPressed() // Now it should be gone. assertEquals(View.GONE, progressBar.visibility) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/ui/BaseSheetActivity.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/ui/BaseSheetActivity.kt index 748ca57aaf6..09e7e99781e 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/ui/BaseSheetActivity.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/ui/BaseSheetActivity.kt @@ -13,6 +13,7 @@ import android.view.WindowInsets import android.view.WindowMetrics import android.widget.ScrollView import android.widget.TextView +import androidx.activity.addCallback import androidx.annotation.DrawableRes import androidx.annotation.StringRes import androidx.annotation.VisibleForTesting @@ -128,9 +129,14 @@ internal abstract class BaseSheetActivity : AppCompatActivity() { } } + val onBackPressedCallback = onBackPressedDispatcher.addCallback { + viewModel.handleBackPressed() + } + viewModel.processing.launchAndCollectIn(this) { isProcessing -> updateRootViewClickHandling(isProcessing) toolbar.isEnabled = !isProcessing + onBackPressedCallback.isEnabled = !isProcessing } // Set Toolbar to act as the ActionBar so it displays the menu items. @@ -190,13 +196,6 @@ internal abstract class BaseSheetActivity : AppCompatActivity() { overridePendingTransition(AnimationConstants.FADE_IN, AnimationConstants.FADE_OUT) } - @Deprecated("Deprecated in Java") - override fun onBackPressed() { - if (!viewModel.processing.value) { - viewModel.handleBackPressed() - } - } - protected fun closeSheet( result: ResultType ) { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentOptionsActivityTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentOptionsActivityTest.kt index d8d5c8795b9..aaba469af40 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentOptionsActivityTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentOptionsActivityTest.kt @@ -12,6 +12,7 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.SavedStateHandle import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider +import androidx.test.espresso.Espresso.pressBack import app.cash.turbine.test import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.common.truth.Truth.assertThat @@ -228,7 +229,7 @@ internal class PaymentOptionsActivityTest { .isTrue() // Navigate back to payment options list - activity.onBackPressed() + pressBack() idleLooper() assertThat(activity.viewBinding.continueButton.isVisible) diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt index efabb5746f8..4d1a6bc60cc 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetActivityTest.kt @@ -300,7 +300,7 @@ internal class PaymentSheetActivityTest { assertThat(activity.viewBinding.message.isVisible).isTrue() assertThat(activity.viewBinding.message.text.toString()).isEqualTo("some error") - activity.onBackPressed() + pressBack() assertThat(activity.viewBinding.message.isVisible).isFalse() } @@ -467,13 +467,13 @@ internal class PaymentSheetActivityTest { assertThat(activity.toolbar.navigationContentDescription) .isEqualTo(context.getString(R.string.back)) - activity.onBackPressed() + pressBack() idleLooper() assertThat(activity.toolbar.navigationContentDescription) .isEqualTo(context.getString(R.string.stripe_paymentsheet_close)) - activity.onBackPressed() + pressBack() idleLooper() // animating out assertThat(activity.bottomSheetBehavior.state) diff --git a/stripecardscan/src/main/java/com/stripe/android/stripecardscan/cardscan/CardScanActivity.kt b/stripecardscan/src/main/java/com/stripe/android/stripecardscan/cardscan/CardScanActivity.kt index 2676c10326f..2e3e2f8be4b 100644 --- a/stripecardscan/src/main/java/com/stripe/android/stripecardscan/cardscan/CardScanActivity.kt +++ b/stripecardscan/src/main/java/com/stripe/android/stripecardscan/cardscan/CardScanActivity.kt @@ -9,6 +9,7 @@ import android.util.Size import android.view.View import android.view.ViewGroup import android.widget.ImageView +import androidx.activity.addCallback import androidx.annotation.RestrictTo import com.stripe.android.camera.CameraPreviewImage import com.stripe.android.camera.framework.Stats @@ -194,6 +195,12 @@ internal class CardScanActivity : ScanActivity(), SimpleScanStateful