【问题标题】:React Native WebView handling android hardware buttonReact Native WebView处理android硬件按钮
【发布时间】:2017-12-25 14:27:45
【问题描述】:

我有一个应用程序(确切地说是 Webview),我正在尝试处理 Android 硬件按钮以从第二个页面返回到第一个页面。我现在的问题是按钮不再关闭应用程序,我不知道该怎么办。这是我的代码:

export default class App extends Component {
  constructor(props) {
  super(props)
    this.state={

     };

this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
}

componentWillMount() {
BackHandler.addEventListener('hardwareBackPress',this.handleBackButtonClick);
}
componentWillUnmount() {    BackHandler.removeEventListener('hardwareBackPress',this.handleBackButtonClick);
}

onNavigationStateChange(navState){
  console.log(JSON.stringify(navState));
  canGoBack = navState.url == initialUrl && !navState.loading
}


handleBackButtonClick() {
     this.refs[WEBVIEW_REF].goBack();
     return true;
} 

【问题讨论】:

    标签: android react-native webview


    【解决方案1】:

    handleBackButtonClick 的返回值决定了应用是否继续运行。

    目前,您总是返回true。相反,您可以检查当前导航状态是否是您想要关闭应用程序的状态,并相应地返回false

    handleBackButtonClick() {
        const canGoBack = someCalculation();
        if(canGoBack)
            this.refs[WEBVIEW_REF].goBack();
        }
        return canGoBack;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 2011-04-02
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      相关资源
      最近更新 更多