[英]How to stop the Android Hardware Back Button from functioning in react-navigation for react-native?
[英]React-Native with Expo: Handle Hardware Back Button
按照這些指示,我設法使后退按鈕強制我的 WebView 導航回最后一頁。
但是我面臨一個副作用:除了返回導航之外,它還退出應用程序,在 Android 中。 以下是我當前的代碼。 有誰知道它有什么問題?
function useBackButton(handler: any) {
useEffect(()=> {
BackHandler.addEventListener("hardwareBackPress", handler)
return () => {
BackHandler.removeEventListener("hardwareBackPress", handler)
}
}, [handler])
}
export default function App() {
const webviewRef = useRef(null)
function backButtonHandler() : any {
console.log("==> Back Pressed", webviewRef.current)
if (webviewRef) {
if (webviewRef.current) {
webviewRef.current.goBack()
}
}
}
useBackButton(backButtonHandler)
return (
<WebView
source={{uri: "http://my-website"}}
style={{ marginTop: 40 }}
ref={webviewRef}
sharedCookiesEnabled
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
正如@Nadia Chibrikova 所指出的,確實缺少“返回真”:
function backButtonHandler() : any {
console.log("==> Back Pressed", webviewRef.current)
if (webviewRef) {
if (webviewRef.current) {
webviewRef.current.goBack()
}
}
return true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.