我正在使用react-native-router-flux
进行反应原生导航。我的反应原生应用程序上有4个场景堆栈。
当应用程序首次在主屏幕上启动时,单击后退按钮会按预期退出应用程序。
但当我导航到任何其他屏幕并返回主屏幕时,后退按钮似乎不再起作用。
这是代码片段:
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();
导航到主屏幕。
有关如何通过导航到其他屏幕返回主屏幕时如何退出应用程序的任何想法?
谢谢。
如果从作为此库的一部分的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}
>
这不是理想的,只是在这里作为一种解决方法,直到在库中修复了错误,因此如果您将来阅读此答案,请检查以确保您确实需要使用此解决方法。