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:
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 );
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 }
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); }
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

