Android 为什么不在片段中调用onActivityResult?
我使用的是OneTap Android Sing-In,我创建了一个包含所有一次点击逻辑的类,我用Hilt在片段中注入该逻辑,它工作正常,但onActivityResult的Android 为什么不在片段中调用onActivityResult?,android,kotlin,Android,Kotlin,我使用的是OneTap Android Sing-In,我创建了一个包含所有一次点击逻辑的类,我用Hilt在片段中注入该逻辑,它工作正常,但onActivityResult的仅在活动中工作,而不在片段中工作 这是触发onActivityResult的函数,它位于我的OneApLogin类中: fun singUp(){ val signUpRequest = createSignInRequest() oneTapClient .beginSignIn(
仅在活动中工作,而不在片段中工作
这是触发onActivityResult的函数,它位于我的OneApLogin类中:
fun singUp(){
val signUpRequest = createSignInRequest()
oneTapClient
.beginSignIn(signUpRequest)
.addOnSuccessListener(context as FragmentActivity) { result ->
try {
startIntentSenderForResult(context,
result.pendingIntent.intentSender, REQ_ONE_TAP,
null, 0, 0, 0, null
)
} catch (e: IntentSender.SendIntentException) {
Log.e(TAG, "Couldn't start One Tap UI: ${e.localizedMessage}")
}
}
.addOnFailureListener(context as Activity) { e ->
Log.d(TAG, e.localizedMessage)
}
}
在这个类中,我还有一个函数:
fun getResult(requestCode: Int, resultCode: Int, data: Intent?) {
//Do stuff with the data
}
然后在片段中,我注入类并调用singUp(),但是如果我在这里没有调用activityresult,那么我在activity中尝试了它,它就工作了
class MainFragment : Fragment() {
@Inject
lateinit var oneTapLogin: OneTapLogin
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentMainBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false)
oneTapLogin.singUp()
return binding.root
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
oneTapLogin.getResult(requestCode, resultCode, data)
}
}
我希望它能在片段中工作,这样我就不必在活动中再次注入类来获得结果,我该怎么做?您的活动的onActivityResult
是否调用super(如果它不识别请求代码)?