【问题标题】:Exit app when clicked back on home screen在主屏幕上单击返回时退出应用程序
【发布时间】:2017-12-20 04:49:44
【问题描述】:

我正在使用 react-native-router-flux 进行本机导航。我的 react-native 应用程序上有 4 个场景堆栈。

当应用首次在主屏幕上启动时,单击后退按钮会按预期退出应用。

但是当我导航到任何其他屏幕并返回主屏幕时,后退按钮似乎不再起作用。

这里是sn-p的代码:

onBackPress() {    
    if (Actions.state.index === 0) {
      return false;
    }
    Actions.pop();
    return true;
}

<Router backAndroidHandler={this.onBackPress}>
    <Scene key="root">
        <Scene key="home" initial component={HomeScreen} />    
        <Scene key="screen2" component={MainScreen} />    
        <Scene key="screen3" component={ScreenSec} />
    </Scene>
</Router>

我在组件中使用Actions.pop(); 导航到主屏幕。

知道当我通过导航到其他屏幕返回主屏幕时如何退出应用程序吗?

谢谢。

【问题讨论】:

    标签: android reactjs react-native react-native-android react-native-router-flux


    【解决方案1】:

    如果您从属于该库的Example 项目中实现reducer,您可以在reducer 中利用该行为。

    const reducerCreate = params => {
    
      const defaultReducer = new Reducer(params);
    
      return (state, action) => {
        // Open this up in your console of choice and dive into this action variable
        console.log('ACTION:', action);
        // Add some lines like this to grab the action you want
        if(action.type === 'Navigation/BACK' && state.index === 0){
          BackHandler.exitApp()
        }
        return defaultReducer(state, action);
      };
    };
    

    并确保减速器绑定到路由器

    <Router
      createReducer={reducerCreate}
      backAndroidHandler={this.onBackPress}
    >
    

    这并不理想,只是在库中修复错误之前作为一种解决方法,因此如果您将来阅读此答案,请检查以确保您确实需要使用此解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-30
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      相关资源
      最近更新 更多