0

Trying to find out why Unresolved reference: registerForActivityResult ? in my class but in my MainActivity it's fine, am I not importing something I should be, or passing something to the function so that it can 'registerForActivityResult'

package com.example.cardgamexxx

import android.app.Activity
import android.app.AlertDialog
import android.net.Uri
import android.text.InputType
import android.widget.EditText
import androidx.activity.result.contract.ActivityResultContracts
import com.example.cardgamexxx.RequesterType.InputRequester_Password
import com.example.cardgamexxx.RequesterType.MessageRequester_Ok
import com.example.cardgamexxx.RequesterType.MessageRequester_YesNo
import com.example.cardgamexxx.RequesterType.MessageRequester_YesNoCancel

class Requester {

    fun openFileRequester(activity: Activity) {
        val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
            // Handle the returned Uri
        }
    }

}
Melony Sharon
  • 91
  • 3
  • 10
  • 1
    try activity.registerForActivityResult – Linh Jul 29 '21 at 01:44
  • That's one of the first things I tried, however Unresolved reference: registerForActivityResult – Melony Sharon Jul 29 '21 at 01:49
  • 2
    The `registerForActivityResult()` methods are implemented in [`ComponentActivity`](https://developer.android.com/reference/androidx/activity/ComponentActivity), not regular `Activity`. You would need to change the parameter to that, or `AppCompatActivity`, which is more likely to be what you're extending anyway, or even the interface `ActivityResultCaller`, if that makes more sense for whatever you're setting up. – Mike M. Jul 29 '21 at 01:53
  • My way of simplifying things down and avoiding code repetition. https://stackoverflow.com/questions/68554654/alert-dialog-simplification/68558905#68558905 – Melony Sharon Jul 29 '21 at 02:37

0 Answers0