Skip to content

PhoneAuthProvider.OnVerificationStateChangedCallbacks are called in destroyed activity #2497

Open
@aromano272

Description

@aromano272

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 4.1.2
  • Firebase Component: com.google.firebase:firebase-auth-ktx:20.0.3
  • Component version: 20.0.3

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

  • Have "Don't keep activies" turned ON
  • Have the user see the Captcha while authenticating with phone

After starting the Phone Auth flow with: PhoneAuthProvider.verifyPhoneNumber(options) the user goes to the captcha(this will destroy the activity because of "Don't keep activities"), when the user comes back the OnVerificationStateChangedCallbacks .onCodeSent will be called in the destroyed activity.

Simplified code of my Auth activity:

class SmsValidationActivity : AppCompatActivity() {

    private val args: SmsValidationActivityArgs by navArgs()

    private var stateIsVerificationInProgress: Boolean = false
    private var stateHasCodeAlreadyBeenSent: Boolean = false
    private var stateVerificationId: String? = null
    private var stateForceResendingToken: PhoneAuthProvider.ForceResendingToken? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.sms_validation_activity)

        if (savedInstanceState != null) onRestoreInstanceState(savedInstanceState)

        // When the user enter the screen the verification automatically starts
        verifyPhoneNumber(stateForceResendingToken)
    }

    private fun verifyPhoneNumber(resendingToken: PhoneAuthProvider.ForceResendingToken?) {
        stateIsVerificationInProgress = true
        if (!stateIsVerificationInProgress) stateHasCodeAlreadyBeenSent = false

        val options = PhoneAuthOptions.newBuilder()
            .setPhoneNumber(args.phone)
            .setTimeout(60L, TimeUnit.SECONDS)
            .setActivity(this)
            .setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                override fun onVerificationCompleted(credential: PhoneAuthCredential) {
                    stateIsVerificationInProgress = false
                }

                override fun onVerificationFailed(ex: FirebaseException) {
                    stateIsVerificationInProgress = false
                }

                override fun onCodeSent(verificationId: String, forceResendingToken: PhoneAuthProvider.ForceResendingToken) {
                    stateVerificationId = verificationId
                    stateForceResendingToken = forceResendingToken
                    stateHasCodeAlreadyBeenSent = true
                }
            })
            .apply {
                if (resendingToken != null) setForceResendingToken(resendingToken)
            }
            .build()

        PhoneAuthProvider.verifyPhoneNumber(options)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean(STATE_IS_VERIFICATION_IN_PROGRESS, stateIsVerificationInProgress)
        outState.putBoolean(STATE_HAS_CODE_ALREADY_BEEN_SENT, stateHasCodeAlreadyBeenSent)
        outState.putString(STATE_VERIFICATION_ID, stateVerificationId)
        outState.putParcelable(STATE_FORCE_RESENDING_TOKEN, stateForceResendingToken)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        stateIsVerificationInProgress = savedInstanceState.getBoolean(STATE_IS_VERIFICATION_IN_PROGRESS)
        stateHasCodeAlreadyBeenSent = savedInstanceState.getBoolean(STATE_HAS_CODE_ALREADY_BEEN_SENT)
        stateVerificationId = savedInstanceState.getString(STATE_VERIFICATION_ID)
        stateForceResendingToken = savedInstanceState.getParcelable(STATE_FORCE_RESENDING_TOKEN)
    }

    companion object {
        const val STATE_IS_VERIFICATION_IN_PROGRESS: String = "STATE_IS_VERIFICATION_IN_PROGRESS"
        const val STATE_HAS_CODE_ALREADY_BEEN_SENT: String = "STATE_HAS_CODE_ALREADY_BEEN_SENT"
        const val STATE_VERIFICATION_ID: String = "STATE_VERIFICATION_ID"
        const val STATE_FORCE_RESENDING_TOKEN: String = "STATE_FORCE_RESENDING_TOKEN"
    }

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions