微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BackHandler 或 webview 返回和退出应用程序

如何解决BackHandler 或 webview 返回和退出应用程序

我正在创建一个 webview,当单击 backHandler 时返回上一页,但我希望当您处于初始状态并单击返回关闭应用程序时,有人可以帮助我!!!

  const [canGoBack,setCanGoBack] = useState(false)
  const [currentUrl,setCurrentUrl] = useState('')
  const webviewRef = useRef(null)

   useEffect(() => {
 
    const backAction = () => { 
      if(webviewRef.current) webviewRef.current.goBack()
      return true;
    };
    
    const backHandler = BackHandler.addEventListener(
      "hardwareBackPress",backAction,);   

    return () => {
      backHandler.remove();
    };
  },[]);

 < WebView 
      
        onLoadStart={() => (showSpinner())}
        onLoadEnd={() => hideSpinner()}
        source = { {  uri : 'https://treinamento.viashopmoda.com.br/?origem=app'  } } 
        ref={webviewRef}
        onNavigationStateChange={navstate => {
          setCanGoBack(navstate.canGoBack)
          setCurrentUrl(navstate.url)
         
        }}
        renderError={() => 
        <View style={styles.loadingOrErrorView}>
        <View style={styles.container}>
        <Text style={{fontSize:25,marginBottom:25,color:'#BEBEBE',textAlign:'center'}}>Não foi possível conectar-se a internet</Text>
        <TouchableOpacity style={styles.botao} onPress={()=>recarregar()}>
          <Text style={{color:'#BEBEBE',fontSize:18,textAlign:'center'}}>Repetir</Text>
        </TouchableOpacity>
      </View>
      </View>    
      }
      />
  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。