React-native-router-flux: Hardware Back closes App on every scene

Created on 15 Sep 2017  路  7Comments  路  Source: aksonov/react-native-router-flux

Version

Tell us which versions you are using:

  • react-native-router-flux v4.0.0-beta.21 (v3 is not supported)
  • react-native v0.47.1

Expected behaviour

Hardware back press should close app only when there is no scene to go back to.

Actual behaviour

App closes on any of the child scene

Steps to reproduce

This is my Router definition
<Router createReducer={reducerCreate} getSceneStyle={getSceneStyle} navBarStyle={styles.navigationBarStyle} backAndroidHandler={backAndroidHandler}>...</Router>

I am passing backAndroidHandler as empty
const backAndroidHandler = () => { console.log('Back Handler'); };
Even though I am not handling back press, the app still closes.
Please help.

Most helpful comment

@tusharv-bidchat

const onBackAndroid = () => {
    return false; // Return true to stay, or return false to exit the app.
};

<Router backAndroidHandler={onBackAndroid}>
</Router>

refer https://github.com/aksonov/react-native-router-flux/blob/master/src/Router.js#L23

All 7 comments

Fixed the issue by using following:
const reducerCreate = params => { const defaultReducer = new Reducer(params); return (state, action) => { console.log('ACTION:', action, Actions.currentScene); if (action.type === 'Navigation/BACK') { console.log(TAG, 'Back Pressed'); if (Actions.currentScene === 'home') { BackHandler.exitApp(); } } if (action.type === 'Navigation/INIT') { Actions.auth(); } return defaultReducer(state, action); }; };

@tusharv-bidchat

const onBackAndroid = () => {
    return false; // Return true to stay, or return false to exit the app.
};

<Router backAndroidHandler={onBackAndroid}>
</Router>

refer https://github.com/aksonov/react-native-router-flux/blob/master/src/Router.js#L23

@tanthanh289 @tusharv-bidchat do we need to write on every scene about the hardware back procedure? because on my case, pressing hardware back on reset scene won't do nothing, while pressing hardware back on normal scene will exit the app

Is there any better solution? or a stable release per se?

@sitompul I did this

const onBackAndroid = () => {
    Actions.pop()
    return true; // Return true to stay, or return false to exit the app.
};

<Router backAndroidHandler={onBackAndroid}>
</Router>

@albinhubsch Thanks i already implemented that. It's working fine now

If you want to have the expected behavior use the following

<Router backAndroidHandler={() => { return Actions.pop(); }}>
</Router>

This will make the back button pops the scenes of the stack without exiting until there are no scenes to pop then the app will exit.

Should work well with latest RNRF Example project.

Was this page helpful?
0 / 5 - 0 ratings