简体   繁体   English

BackAndroid无法卸载-React Native(Android)

[英]BackAndroid not unmounting - React Native (Android)

I am working with React Native 0.29 with Android. 我正在使用Android的React Native 0.29。 For a particular view/activity/screen of my app, I want to add an event listener for BackAndroid button, which is available with react native. 对于我的应用程序的特定视图/活动/屏幕,我想为BackAndroid按钮添加事件监听器,该按钮可与react native一起使用。 I already have a global BackAndroid event listener added to my app (in my index.android.js file) which pop out any view from the stack if it's not the main screen. 我已经有一个全局BackAndroid事件侦听器添加到我的应用程序中(在我的index.android.js文件中),如果不是主屏幕,它将从堆栈中弹出任何视图。

The event listener is activated with componentDidMount() lifecycle method and it works. 事件侦听器通过componentDidMount()生命周期方法激活,并且可以正常工作。 It override the global one and works as expected. 它会覆盖全局变量并按预期工作。 Now the problem is, it doesn't get removed when componentWillUnmount() lifecycle method get fired. 现在的问题是,当componentWillUnmount()生命周期方法被触发时,它不会被移除。 So when back from that particular screen, the event listener still remains and cause trouble. 因此,当从该特定屏幕返回时,事件侦听器仍然存在并引起麻烦。 Here is what I did: 这是我所做的:

componentDidMount() {
  BackAndroid.addEventListener('backBtnPressed', this._handleBackBtnPress.bind(this))
}

componentWillUnmount() {
  BackAndroid.removeEventListener('backBtnPressed', this._handleBackBtnPress.bind(this))
}

I don't understand why it's not working. 我不明白为什么它不起作用。 Please help me to understand why it's not working and what should I do to solve this issue. 请帮助我了解为什么它不起作用以及我应该怎么做才能解决此问题。

I spend hours to solve this problem. 我花了几个小时来解决这个问题。 I think it would help other developers if I share this. 我认为如果我分享这一点,将会对其他开发人员有所帮助。

The main problem here is with the .bind(this) statement, bind always returns a new function. 这里的主要问题是.bind(this)语句, bind总是返回一个新函数。 So in this code, this._handleBackBtnPress.bind(this) are not same functions in addEventListener and removeEventListener . 因此,在这段代码中, this._handleBackBtnPress.bind(this)addEventListenerremoveEventListener中不是相同的函数。 They are different functions referring different first-class objects. 它们是引用不同的一流对象的不同功能。 That's why removeEventListener is not removing the listener. 这就是为什么removeEventListener不删除侦听removeEventListener的原因。

To solve this issue, we can add the following statement to our constructor method - this._handleBackBtnPress = this._handleBackBtnPress.bind(this) and remove .bind(this) from both addEventListener and removeEventListener . 要解决此问题,我们可以在构造函数方法中添加以下语句this._handleBackBtnPress = this._handleBackBtnPress.bind(this)然后从addEventListenerremoveEventListener删除.bind(this) So our code will look something like this: 因此,我们的代码将如下所示:

constructor(props) {
  super(props)
  this._handleBackBtnPress = this._handleBackBtnPress.bind(this)
}

componentDidMount() {
  BackAndroid.addEventListener('backBtnPressed', this._handleBackBtnPress)
}

componentWillUnmount() {
  BackAndroid.removeEventListener('backBtnPressed', this._handleBackBtnPress)
}

Now both of them will refer same function and will work as expected. 现在,它们两个将引用相同的功能,并将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM