Khush Parmar
Khush Parmar

Reputation: 561

ActivityResultLauncher provides Result Cancelled as ActivityResult even when user turn on the location

I am redirecting user to the location service setting when the location is turned off of the device using below code,

private void turnOnLocation() {
        Toast.makeText(getApplicationContext(), "Please enable the location to perform app functionality!", Toast.LENGTH_LONG).show();
        Intent enableLocationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        enabaleLocationLauncher.launch(enableLocationIntent);
    }

and then handling the launcher as below code,

ActivityResultLauncher<Intent> enabaleLocationLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    // Here it returns RESULT_CANCELED every time even when the user turn on the location
                }
            }
    );

When user turn on the location it triggers the onActivityResult method but the ActivityResult returns as RESULT_CANCELED.

Any thought on this..

I tried debugging the code. I think code is fine there might be any other issue. I want some thought on this..

Upvotes: 1

Views: 86

Answers (0)

Related Questions