BackHandler 'HardwareBackPress работает только один раз | Реагировать родной - PullRequest
0 голосов
/ 01 ноября 2018

Мне нужно обработать Back Press на устройстве Android, но мой BackHandler работает только один раз.

Вот мой фрагмент кода -

 componentWillUnmount() {
        BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
    }
    componentDidMount() {
        BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
    }

        handleBackPress = () => {

            console.log("handleBackPress Called on RootMenuView : current View ? : " + this.state.mainView);

            if (this.state.mainView === "Main"){
                return false;
            } else{
                this.state.mainView = "Main";
                this.forceUpdate();
                return true;
            }

        }

Мне нужно обработать это в представлении меню, так как мне нужно показать компонент по умолчанию при повторном нажатии других компонентов.

1 Ответ

0 голосов
/ 01 ноября 2018
 componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
   }
   componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
 }

    handleBackPress = () => {

        console.log("handleBackPress Called on RootMenuView : current View ? : " + this.state.mainView);

        if (this.state.mainView === "Main"){
            return false;
        } else{
             //not this
            //this.state.mainView = "Main";
            // use this
           this.setState({mainView: "Main"})
            this.forceUpdate();
            return true;
        }

    }
...