Cannot resolve method 'registerForActivityResult'

Issue

I am building a custom capacitor plugin to fetch the user’s phone numbers. I am using capacitor 3 with Ionic 6.

I found a solution which is not deprecated and is a lot recent to fetch the user’s phone numbers.

here is my code to get the phone number –

private void requestHint() {
        HintRequest hintRequest = new HintRequest.Builder()
                .setPhoneNumberIdentifierSupported(true)
                .build();
        PendingIntent intent = Credentials.getClient(getActivity()).getHintPickerIntent(hintRequest);
        IntentSenderRequest.Builder intentSenderRequest = new IntentSenderRequest.Builder(intent.getIntentSender());
        hintLauncher.launch(intentSenderRequest.build());
    }

ActivityResultLauncher<IntentSenderRequest> hintLauncher = registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(),
            result -> {
                if(result!=null && result.getData()!=null){
                    Intent data = result.getData();
                    Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
                    String phoneNum = credential.getId();
                }
            });

But I am running across an error on Android Studio "Cannot resolve method ‘registerForActivityResult’ in ‘NumberPluginPlugin‘"

What am I missing here?
As suggested by some folks online I have added the following dependencies –

implementation "androidx.fragment:fragment:1.4.1"
implementation "androidx.activity:activity:1.4.0"
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"

Still I am not sure what is going wrong here.

Solution

registerForActivityResult can be called inside Activity or Fragment, not some custom NumberPluginPlugin class (which doesn’t extends mentioned above classes)

Answered By – snachmsm

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *