Checkout page remains logout after first load

Topic summary

A developer is experiencing an issue with the Shopify Android Buy SDK and Checkout SDK where user login data persists on the first checkout page load but disappears on subsequent loads.

Technical Details:

  • Using cartCreate mutation with buyerIdentity containing customerAccessToken and email
  • Access token and cart ID are stored in SharedPrefs
  • First checkout correctly shows logged-in user details
  • Second and subsequent checkouts fail to load user data

Code Implementation:

  • Creates cart with buyer identity when access token exists
  • Sets customer access token and email in CartBuyerIdentityInput
  • Initiates checkout using the returned checkoutUrl
  • Also includes logic for updating buyer identity with delivery address preferences

Current Status:
The issue remains unresolved with no responses or solutions provided yet. The developer has shared their complete implementation code for review.

Summarized with AI on November 9. AI used: claude-sonnet-4-5-20250929.

I am developing mobile app using shopify android buy sdk and checkout sdk . When i am opening checkout then for first time checkout page remaing logged in with user detail but when it is openend second time then it does not loads the logged in user data.

Below is my code.

val accessToken = SharedPrefs.getInstance(requireContext()).getAccessToken()
        val cartId = SharedPrefs.getInstance(requireContext()).getCartId()
        val email = SharedPrefs.getInstance(requireContext()).getEmail()
        CoroutineScope(Dispatchers.Main).launch {
            if (cartId.isNullOrEmpty()) {
                val cartCreateInput = CartInput()
                if (!accessToken.isNullOrEmpty()) {
                    val buyerIdentity = Storefront.CartBuyerIdentityInput()
                        .setCustomerAccessToken(accessToken)
                        .setEmail(email)
                    cartCreateInput.setBuyerIdentity(buyerIdentity)
                }
                cartCreateInput.setLines(mutableListOf(Storefront.CartLineInput(ID(selectedVariant))))
                val mutation = Storefront.mutation { mutation ->
                    mutation.cartCreate({ arg -> arg.input(cartCreateInput) }) { createPayload ->
                        createPayload.cart { cart ->
                            cart.createdAt()
                            cart.updatedAt()
                            cart.checkoutUrl()
                        }
                            .userErrors { error ->
                                error
                                    .field()
                                    .message()
                            }
                    }
                }
                val call = ShopifyClient.client.mutateGraph(mutation)

                call.enqueue { result ->

                    when (result) {

                        is GraphCallResult.Success -> {
                            val response = result.response
                            val data = response.data?.cartCreate
                            if (data?.cart?.id != null) {
                                SharedPrefs.getInstance(requireContext()).saveCartId(data.cart.id.toString())
                                if (withBuyNow) {
                                    if (data.cart?.checkoutUrl != null) {
                                        Handler(Looper.getMainLooper()).post {
                                            initiateCheckout(requireContext(), requireActivity(), data.cart?.checkoutUrl.toString())
                                        }
                                    }
                                } else {
                                    showSnackBar(binding.root, "Product added to cart.")
                                }
                            }
                            toggleCartLoader(false)
                        }

                        is GraphCallResult.Failure -> {
                            val error = result.error
                            Log.e("Customer Login", " $error")
                            toggleCartLoader(false)
                        }

                    }

                }

            } else {
                val buyerIdentity = Storefront.CartBuyerIdentityInput()
                if (!accessToken.isNullOrEmpty()) {
                    buyerIdentity.setEmailInput(Input.value(email))
                    buyerIdentity.setCustomerAccessToken(accessToken)
                    val defAdd = fetchDefaultAddress(accessToken)
                    val shippingAddressInput = Storefront.MailingAddressInput().apply {
                        defAdd?.let {
                            firstName = it.firstName
                            lastName = it.lastName
                            address1 = it.address1
                            city = it.city
                            country = it.country
                            province = it.province
                            zip = it.zip
                            phone = it.phone
                        }
                    }
                    buyerIdentity.setDeliveryAddressPreferences(listOf(Storefront.DeliveryAddressInput().setDeliveryAddress(shippingAddressInput)))

                }
                val cartCreateInput = Storefront.CartLineInput(ID(selectedVariant))
                val mutation = Storefront.mutation { mutation ->
                    mutation.cartBuyerIdentityUpdate(ID(cartId), buyerIdentity) {
                        it.cart {
                            it.buyerIdentity {
                                it.email()
                            }
                        }
                    }.cartLinesAdd(mutableListOf(cartCreateInput), ID(cartId)) {
                        it.cart {
                            it.updatedAt()
                            it.checkoutUrl()
                        }
                    }
                }

                val call = ShopifyClient.client.mutateGraph(mutation)
                call.enqueue { result ->

                    when (result) {

                        is GraphCallResult.Success -> {
                            val response = result.response
                            val data = response.data?.cartLinesAdd

                            if (data?.cart?.id != null) {
                                Log.e("Cart 0", " ${data?.cart?.buyerIdentity?.email}")
                                if (withBuyNow) {
                                    if (data.cart?.checkoutUrl != null) {
                                        Handler(Looper.getMainLooper()).post {
                                            initiateCheckout(requireContext(), requireActivity(), data.cart?.checkoutUrl.toString())
                                        }
                                    }
                                } else {
                                    showSnackBar(binding.root, "Product added to cart.")
                                }
                            }

                            Log.e("Cart 1", " ${response.data?.cartBuyerIdentityUpdate?.cart?.buyerIdentity?.email}")
                            toggleCartLoader(false)
                        }

                        is GraphCallResult.Failure -> {
                            val error = result.error
                            toggleCartLoader(false)
                            Log.e("Cart", " $error")
                        }

                    }

                }

            }
        }