Preventing hardware back button android for React Native

后端 未结 9 1610
醉梦人生
醉梦人生 2020-12-08 19:05

I want to prevent the user from going back to the previous screen. So I added code, but this does not work. Are there any solutions for this? The alert pop up is seen but \"

相关标签:
9条回答
  • 2020-12-08 19:40

    Disable back button for module react-navigation, use hook useFocusEffect

    const hardwareBackPressCustom = useCallback(() => {
            return true;
        }, []);
    
    useFocusEffect(() => {
        BackHandler.addEventListener('hardwareBackPress', hardwareBackPressCustom)
        return () => {
          BackHandler.removeEventListener('hardwareBackPress', hardwareBackPressCustom)
        };
      }, []);
    
    0 讨论(0)
  • 2020-12-08 19:42

    Try this Disable back button by just returning true

    import {BackAndroid} from 'react-native';
    
    componentWillMount() {
       BackAndroid.addEventListener('hardwareBackPress', () => {return true});
    } 
    
    0 讨论(0)
  • 2020-12-08 19:42

    Onclick of hardware back button OnBackPressed callback gets called

    you can just remove super declaration in onBackPressed call back.

     @Override
        public void onBackPressed() {
    
        }
    
    0 讨论(0)
提交回复
热议问题