chayan das
chayan das

Reputation: 96

Type mismatched : Activity (for callback binding)

I am trying to add firebase phone OTP verification in a fragment and I got stuck at .setActivity(...) 1

when in an activity we use "this", but in a fragment what to use?

private fun sendVerificationcode(number: String) {
    val options = PhoneAuthOptions.newBuilder(firebaseAuth)
        .setPhoneNumber(number)       // Phone number to verify
        .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
        .setActivity(this)                 // Activity (for callback binding)
        .setCallbacks(mCallBacks)          // OnVerificationStateChangedCallbacks
        .build()
    PhoneAuthProvider.verifyPhoneNumber(options)
}

I also referred to this stackoverflow solutionbut didn't help

Upvotes: 0

Views: 87

Answers (2)

eimmer
eimmer

Reputation: 1709

It looks like your call to getActivity() is returning an optional, which by definition can be null. The .setActivity() method does not allow null values to be passed in. Validate getActivity() doesn't return null before you proceed.

getActivity().let{ activity -> 
    val options = PhoneAuthOptions.newBuilder(firebaseAuth)
    .setPhoneNumber(number)       // Phone number to verify
    .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
    .setActivity(activity)                 // Activity (for callback binding)
    .setCallbacks(mCallBacks)          // OnVerificationStateChangedCallbacks
    .build()
    PhoneAuthProvider.verifyPhoneNumber(options)
}

Upvotes: 1

Ranjeet Chouhan
Ranjeet Chouhan

Reputation: 694

You can use any of one in fragment.

requireActivity() //for fragment
getActivity() as Activity // in Kotlin

Upvotes: 1

Related Questions