Appstate keep on getting change in React native in Android

后端 未结 2 953
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 23:24

I am working on React native project and there I am taking location permissions. Also I have to track location permissions always like if user has given permission access af

相关标签:
2条回答
  • 2020-12-31 23:51

    You can use a flag that check whether app should handle background or it's just a permission call.

    const shouldHandleBackground = useRef(true)
    
    const handler = (state) => { 
        if (state === 'active' && shouldHandleBackground.current) { 
            doStuff() 
        }
    }
    
    // when calling for permisson make the flag false
    
    shouldHandleBackground.current = false
    await Android.permission.LocationPermission()
    shouldHandleBackground.current = true
    

    and after permission request you can make flag true

    0 讨论(0)
  • 2021-01-01 00:01

    I had the same problem. Do not use AppState. Is faulty.

    the problem lies within RN's definition of "background". react-native uses android's activity (the holder of the UI thread and where your UI lives) onPause callback as the trigger for sending the "background" signal. But, onPause is called everytime SOMETHING comes in front of your activity's view hierachy, like Dialogs (like the permission box), other activities (like a file picker), etc; for android react-native, "background" means "shadowed by a foreign UI element/android task" rather than "paused and sent to background to do something else", thus causing the loops you see. The shortest solution is to override onPause in your ReactActivity, and add control conditions to make sure super.onPause is only called when you are actually going to background, like checking your task stack, or if the permission dialog is being called, so you avoid this kind of loop/faulty call. A second option would be to provide your own app lifecycle event instead, with clear triggering conditions.

    0 讨论(0)
提交回复
热议问题