In the current version I've installed (3.35.0), there's provision to assign custom backButtonHandler for the Router. While this is useful, my app requires custom behavior in certain screens.
In the previous version, I used to have a global BackAndroid event listener which gets the component currently in foreground from the navigation stack with Actions.currentRouter.stack. In the component's prototype chain, I had a onBackButton() method which handles custom behavior.
The new Action API doesn't have currentRouter exposed. I can addEventListener in every component and removeEventListener on componentWillUnmount. But, is there a cleaner way to achieve this?
If not, I propose that we either:
backButtonHandler prop in Scene API.What do you guys think?
removed the contents of this post, made a separate issue for You are already in the root scene and put it there.
Hey, i'm having the same problem and it seems that removing the event handler with the componentWillUnmount method doesnt work with this library, did you find any other solution?
You should upgrade RN to 0.32.
With this, you can have the global handler for most scenes. For those special scenes, you can add BackAndroid.addEventListener('hardwareBackPress', this.handleAndroidBack) in them, RN will call the later listener callback first, the global handler would not be called if you return true in the later listener callback. I think it's a better solution. Just remember to remove listener callback.
Most helpful comment
You should upgrade RN to 0.32.
With this, you can have the global handler for most scenes. For those special scenes, you can add
BackAndroid.addEventListener('hardwareBackPress', this.handleAndroidBack)in them, RN will call the later listener callback first, the global handler would not be called if youreturn truein the later listener callback. I think it's a better solution. Just remember to remove listener callback.