androidcapacitor

Cannot resolve method 'registerForActivityResult'


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 AppCompatActivity or Fragment (when you are using AndroidX), not some custom NumberPluginPlugin class (which doesn't extend the classes mentioned above).

    Note that there's no such method in common Activity, only AndroidX extension of Activity brings this feature.

    HERE you can find some article about how to implement this new way (also with comparison to the old, deprecated one).