0

I am stuck with a problem, where I need stack length on BackAndroid Press before I decide, whether I want to pop the scene or not. Is there a way to get stack length?

BackAndroid.addEventListener('hardwareBackPress', this.exitPress.bind(this));

Basically on exitPressed, I want to check if stack length is 1, if it is, I want to redirect it to another scene, any help is appreciated.

Namit Singal
  • 1,506
  • 12
  • 26

1 Answers1

0

Assuming you're using Navigator your index.android.js file should look like:

export default class Foo extends React.Component {

    constructor(props) {
        super(props);
        this._navigator = null;
    }

    renderScene(route, navigator) {
        if(!this._navigator){
            this._navigator = navigator;
            BackAndroid.addEventListener('hardwareBackPress', () => {
                if (this._navigator && this._navigator.getCurrentRoutes().length > 1) {
                    this._navigator.pop();
                    return true;
                }

                return true;
            });
        }

        return React.createElement(component, { ...this.props, ...route.passProps, route, navigator } );
    }

    render() {
        return (
            <Navigator
                initialRoute={...}
                configureScene={...}
                renderScene={(route, nav) => {return this.renderScene(route, nav)}}
            />
        );
    }
}

AppRegistry.registerComponent('Foo', () => Foo);

If you're not using Navigator IDK how you can accomplish this because hardwareBackPress doesn't know anything about the position on the stack.

Eldelshell
  • 6,683
  • 7
  • 44
  • 63