I a trying to request for location permission in Android
but as I call for permission the AppState 'change' handler call. and it act like app was in background
react-native info output:
System:
OS: macOS Mojave 10.14.6
CPU: (4) x64 Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
Memory: 22.64 MB / 8.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 12.13.0 - /usr/local/bin/node
Yarn: 1.22.0 - /usr/local/bin/yarn
npm: 6.13.7 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
Android SDK:
API Levels: 23, 25, 27, 28, 29
Build Tools: 27.0.3, 28.0.3, 29.0.2
System Images: android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.6010548
Xcode: 11.3.1/11C505 - /usr/bin/xcodebuild
npmPackages:
@react-native-community/cli: Not Found
react: 16.9.0 => 16.9.0
react-native: 0.61.5 => 0.61.5
react-native-permissions: ^2.0.10
npmGlobalPackages:
*react-native*: Not Found
Library version: 2.0.10
useEffect(() => {
makeLocationPermission()
AppState.addEventListener('change', appStateHandler)
initialRender.current = true
return () => {
AppState.removeEventListener('change', appStateHandler)
}
}, [scope])
const appStateHandler = async (state) => {
if (state === 'active') {
if (!scope.isGranted) {
await makeLocationPermission()
}
}
}
It's normal. The permissions alert is not in your application View.
@zoontek but it cause bug. how can I handle it.
for my case I want to open Settings page and form the user to manually enable some permissions and when app state is 'active' I want to check whether user enabled that specific permission or not.
You don't need to listen for AppState changes if the result of the permission check is not blocked (because a system alert will appear to ask permission, no need to open settings).
Subscribe to it only if you trigger openSettings (just before calling the function) and unsubscribe when the app regain focus.
I have both case. alert and openSettings. I just fix it with a flag. but seems a little hacky
@zoontek I am not sure if this is entirely true. Re-checking after AppState changes might still make sense as a user can change permissions even if he/she does not use the openSettings call, but by navigating to those settings. Therefore, I would still check for AppState changes.
In my case I am not checking for location permissions, but for notifications. Still, there are ways to handle it. I just wanted to note that there might be valid use cases.
Most helpful comment
@zoontek I am not sure if this is entirely true. Re-checking after AppState changes might still make sense as a user can change permissions even if he/she does not use the
openSettingscall, but by navigating to those settings. Therefore, I would still check for AppState changes.In my case I am not checking for location permissions, but for notifications. Still, there are ways to handle it. I just wanted to note that there might be valid use cases.