【问题标题】:React native : Android device back buttonReact native:Android 设备后退按钮
【发布时间】:2018-01-20 13:43:53
【问题描述】:

我在 android 设备的后退按钮上遇到了一个问题。当我登录应用程序并转到主屏幕并从主屏幕时,如果我按下设备返回按钮,那么它会再次重定向到我的登录屏幕而不是背景。

请帮助我在应用程序成功登录应用程序(即从主屏幕)后按返回按钮时应用程序如何进入后台状态。

这是我的代码,

Login.js

//Click on login button call this method
  home()
  {
    const { navigate } = this.props.navigation;
    navigate('Home', { name: 'Home'});

  }

Home.js:

constructor(props) {
    super(props)
    this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
}

componentWillMount()
{
  console.disableYellowBox = true;
  console.log(this.props.navigation.state.routeName);

    this.callapi();
  }

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

handleBackButtonClick() {
  if (this.props.navigation.state.routeName == 'Home') {
  //  this.props.navigation.goBack(null);
//  BackHandler.exitApp()
this.props.navigation.dispatch(resetAction)
    }
    return false;

}

【问题讨论】:

  • 你用的是什么类型的路由器?
  • 我正在使用 react-navigation。

标签: react-native react-navigation


【解决方案1】:

这是因为当登录返回 true 时,您将用户推送到主页。您必须重置路由器的堆栈。

我个人使用 Wix 的react-native-navigation,从未使用过react-navigation。但是通过查看他们的文档,它表明您可以通过添加此代码块来重置堆栈:

import { NavigationActions } from 'react-navigation'

const resetAction = NavigationActions.reset({
  index: 0,
  actions: [
    NavigationActions.navigate({ routeName: 'Profile'})
  ]
})
this.props.navigation.dispatch(resetAction)

更多信息可以找到here

【讨论】:

  • 做同样的事情,但不起作用,我不知道为什么会这样。
  • 如果您不发布一些代码,我将无法完全帮助您。
  • 好的,这是我的代码: Login.js: home() { const { navigate } = this.props.navigation;导航('家',{名称:'家'}); }
  • Home.js: constructor(props) { super(props) this.handleBackButtonClick = this.handleBackButtonClick.bind(this); } componentWillMount() { console.disableYellowBox = true; console.log(this.props.navigation.state.routeName); } componentWillUnmount() { BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick); } handleBackButtonClick() { if (this.props.navigation.state.routeName == 'Home') { // this.props.navigation.goBack(null); // BackHandler.exitApp() this.props.navigation.dispatch(resetAction) } }
  • 也添加 login.js。
猜你喜欢
  • 2017-12-15
  • 1970-01-01
  • 2019-05-14
  • 2021-04-03
  • 1970-01-01
  • 2021-11-21
  • 2013-09-30
  • 2020-05-27
相关资源
最近更新 更多