K. Ajay
K. Ajay

Reputation: 417

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.

Upvotes: 14

Views: 18564

Answers (9)

Talha &#199;
Talha &#199;

Reputation: 114

If you updated required libraries and still encountering unresolved reference error, maybe some libraries overriding the latest classes with old version classes. You should check and compare your gradle versions and the current versions first. Then you can fix it by forcing to use the lastest version like below.

In app level build.gradle:

implementation "androidx.activity:activity-ktx:1.9.3"
implementation 'androidx.fragment:fragment-ktx:1.8.5'
implementation 'androidx.appcompat:appcompat:1.7.0'

In project level build.gradle:

allprojects {
configurations.configureEach {
    resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'androidx.fragment' && details.requested.name == 'fragment') {
            details.useVersion '1.8.5'
        }
    }
}

}

Upvotes: 0

Hassaan J.
Hassaan J.

Reputation: 150

For me, the issue was that my Fragment class was extended from android.app.Fragment

Instead of androidx.fragment.app.Fragment

android.app.Fragment does not have registerForActivityResult callback.

Upvotes: 0

Harish Reddy
Harish Reddy

Reputation: 992

First of all, make sure you are implementing this inside the activity, Only inside the activity, not in the composable or any other place.

  val requestPermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { results ->
        // Handle permission requests results
        // See the permission example in the Android platform samples: https://github.com/android/platform-samples
    }

Upvotes: 0

pny_hk
pny_hk

Reputation: 141

I resolved my case of "Unresolved reference: registerForActivityResult" by checking the Project Structures->Dependencies->->androidx.activity:activity. It is caused by using both 1.6.0 and 1.8.0 version that was caused by implementation library further deep in the path. enter image description here

After I upgrade the implementation library from github as below such that both activity are now 1.8.x. The issue is resolved. enter image description here

Upvotes: 2

snachmsm
snachmsm

Reputation: 19253

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).

Upvotes: 6

Komang
Komang

Reputation: 41

Use AppCompatActivity instead of Activity

something like : public class ActivityMain extends Activity ----> public class ActivityMain extends AppCompatActivity

Upvotes: 4

mc_hand_os
mc_hand_os

Reputation: 41

try update appcompat to newest version

implementation 'androidx.appcompat:appcompat:1.5.1'

Upvotes: 3

Amer Al zibak
Amer Al zibak

Reputation: 1951

you may need to add this (if not exist) into your app build.gradle :

implementation 'androidx.activity:activity-ktx:1.6.0'

Upvotes: 0

Pablo Chvx
Pablo Chvx

Reputation: 1931

I had the registerForActivityResult method inside a Fragment and still didnt work. This answer helped me: https://stackoverflow.com/a/66209780/1034622

Upvotes: 0

Related Questions