Autofill service enable via Activity Results API

200 Views Asked by At

I'm try to register an ActivityResultContract for android.provider.Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE using kotlin and AndroidX:

class AutofillContract() : ActivityResultContract<Any?,ActivityResult>() {
    override fun createIntent(context: Context, input: Any?): Intent
        = Intent(android.provider.Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE)

    override fun parseResult(resultCode: Int, intent: Intent?): ActivityResult
        = ActivityResult(resultCode, intent)
}

val afrl = registerForActivityResult(AutofillContract()) {
    if (it.resultCode == RESULT_OK) ...
    else ...
}

However, when I try to use it:

afrl.launch(AutofillContract().createIntent(this, null))

I get IllegalArgumentException: Can only use lower 16 bits for requestCode, which I presume was triggered by an internal startActivityForResult() call.

I haven't used a custom ActivityResultContract before, and although it seems simple it also seems a bit sketchy to me -- I'm not sure if regarding the input as irrelevant (Any?) is the way to go, but it does seem irrelevant in this case (the first version used Intent as the input type but there doesn't seem to be a point, and the problem, "Can only use lower 16 bits..." was the same).

I'm using androidx.activity:activity-ktx:1.2.0-alpha08.

1

There are 1 best solutions below

1
On BEST ANSWER

As per this issue, you get that error when you are using an older version of Fragments.

You must also upgrade your version of Fragments to androidx.fragment:fragment-ktx:1.3.0-alpha08.