React-native-router-flux: Question: Scene specific back button behavior [Android]

Created on 6 Sep 2016  路  3Comments  路  Source: aksonov/react-native-router-flux

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:

  1. Have backButtonHandler prop in Scene API.
  2. Pass some extra arguments like currentComponent, to the back button handler's call.

What do you guys think?

Versions

  • react-native-router-flux - 3.35.0
  • react-native 0.28.0

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 you return true in the later listener callback. I think it's a better solution. Just remember to remove listener callback.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VictorK1902 picture VictorK1902  路  3Comments

YouYII picture YouYII  路  3Comments

maphongba008 picture maphongba008  路  3Comments

wootwoot1234 picture wootwoot1234  路  3Comments

xnog picture xnog  路  3Comments