You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fragment中onRequestPermissionsResult不触发问题求助

Hey there! Let's troubleshoot why your onRequestPermissionsResult isn't triggering after the user accepts location permissions in your PerdidosMapa_Fragment. Here are the most common fixes and checks to resolve this:

1. Ensure you're requesting permissions from the Fragment itself

If you're using getActivity().requestPermissions(...) to ask for permissions, the result will be sent to the parent Activity's onRequestPermissionsResult instead of the Fragment's. Make sure you call the Fragment's own requestPermissions method:

// Correct way in Fragment
private static final int LOCATION_PERMISSION_CODE = 100;

// When you need to request permissions
requestPermissions(
    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
    LOCATION_PERMISSION_CODE
);
2. Don't forget to call super in the Activity's permission callback

If your parent Activity overrides onRequestPermissionsResult, you must call super.onRequestPermissionsResult(...)—otherwise, the Fragment will never receive the result. Here's how to fix that:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    // Your Activity-specific logic (if any) goes here
}
3. Double-check your request code consistency

Make sure the request code you use when calling requestPermissions matches the one you check in onRequestPermissionsResult. A typo here will cause the callback to ignore the result:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == LOCATION_PERMISSION_CODE) { // Use the SAME constant as the request
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted, execute your logic
            Log.i("Permission", "Location access approved!");
        }
    }
}

The old onRequestPermissionsResult system is deprecated, and using the Activity Result Contracts is more reliable. It eliminates callback passing issues entirely. Here's how to implement it in your Fragment:

private ActivityResultLauncher<String> locationPermissionLauncher;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Register the permission launcher
    locationPermissionLauncher = registerForActivityResult(
        new ActivityResultContracts.RequestPermission(),
        isGranted -> {
            if (isGranted) {
                // Permission accepted, do your work
                Log.i("Permission", "Location permission granted!");
            } else {
                // Permission denied, handle accordingly
                Log.i("Permission", "Location permission denied.");
            }
        }
    );
}

// Call this method to request permission
private void askForLocationPermission() {
    locationPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION);
}
5. Check if the permission is already granted first

If the user has already granted the permission, the request dialog won't pop up, so the callback won't trigger. Always verify the permission status before requesting:

if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    // Permission already exists, skip request and run your logic
    return;
}
// Only request permission if it's not granted
requestPermissions(...);

If none of these fixes work, feel free to share the full code for how you're requesting permissions and your complete onRequestPermissionsResult method—we can dig deeper!

内容的提问来源于stack exchange,提问作者Budha 600

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:40:14