React Native - Device back button handling

后端 未结 9 2135
感情败类
感情败类 2020-12-02 18:33

I want to check if there are more than one screens are on stack when device back button is hit. If yes, I want to show previous screen and if no, I want to exit app.

9条回答
  •  我在风中等你
    2020-12-02 19:04

    I used flux for navigation.

        const RouterComp = () => {
    
        let backLoginScene=false;
    
        return (
    
             {
                const back_button_prohibited = ['login','userInfo','dashboard'];
                if (back_button_prohibited.includes(Actions.currentScene) ) {
                    if (backLoginScene == false) {
                        ToastAndroid.show("Click back again to exit.", ToastAndroid.SHORT);
                        backLoginScene = !backLoginScene;
                        setTimeout(() => {
                            backLoginScene = false;
                        }, 2000);
                        return true;
                    } else {
                        backLoginScene = false;
                        BackHandler.exitApp();
                    }
                    return false;
                }}}>
                
                    
                        
                        
                    
    
                    
                        
                        
    
                    
    
    
    
                
            
        )
    }
    
    export default RouterComp;
    

提交回复
热议问题