Handling back button in React Native, Navigator on Android

前端 未结 6 802
旧巷少年郎
旧巷少年郎 2021-02-01 14:45

I have a Navigator in an Android react native application.

I\'m using navigator.push() to navigate to a different page. It would seem natural t

6条回答
  •  野的像风
    2021-02-01 15:37

    I have created a GitHub repo that will provide you a sample project of how to handle the Android Back Button.

    You can clone / download the repo at:

    Android Back Button Sample Project

    But here are some sample codes on how I handle the android back button

    The following code is for my initial screen when user starts my app. Pressing the back button button here would show an alert of which will ask the user whether the user would like to leave the app.

    import React, {Component} from 'react';
    import {View,Text,Button,BackHandler,Alert} from 'react-native';
    import {NavigationActions} from 'react-navigation';
    
    export default class InitScreen extends Component {
        _focusDoneSubscribe;//declaring variable that will be listener for the back button when in focus 
        _blurGoingSubscribe;//declaring variable that will be listener for the back button when blur 
    
        //the following will remove the navigation bar at the top
        static navigationOptions = {
            header: null,
            title: 'Welcome',
        };
    
        constructor(props)
        {
            super(props);
            this._focusDoneSubscribe = props.navigation.addListener('didFocus', payload =>
                BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
            );//this assigns the listener
        }
        //functions to handle back button events
        componentDidMount()
        {
            this._blurGoingSubscribe = this.props.navigation.addListener('willBlur', payload =>
                    BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
                );
        }
    
        onBackButtonPressAndroid = () => {
            Alert.alert(
                'Exiting App',
                'Confirm quitting the app?',
                [
                    {text: 'CANCEL', style: 'cancel'},
                    {text: 'STOP ASKING AND QUIT', onPress: () => BackHandler.exitApp()}
                ],
                {cancelable: false},
            );
            return true;
        };
    
        componentWillUnmount()
        {
            this._focusDoneSubscribe && this._focusDoneSubscribe.remove();
            this._blurGoingSubscribe && this._blurGoingSubscribe.remove();
        }
        //actual render
        render(){
            const { navigate } = this.props.navigation;
            return (
                
                    
                        Navigation BackHandler Tutorial
                        Initial Screen
                    
                    
                        

    The following code is a screen for which when the user presses the back button, it go back the previous screen:

    import React, {Component} from 'react';
    import {View,Text,Button,BackHandler} from 'react-native';//declaring variable that will be listener for the back button when in focus 
    import {NavigationActions} from 'react-navigation';//declaring variable that will be listener for the back button when blur 
    
    export default class ScreenOne extends Component {
        _focusDoneSubscribe;
        _blurGoingSubscribe;
    
        //the following will remove the navigation bar at the top
        static navigationOptions = {
            header: null,
            title: 'ONE',
        };
    
        constructor(props)
        {
            super(props);
            this._focusDoneSubscribe = props.navigation.addListener('didFocus', payload =>
                BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
            );//this assigns the listener
        }
        //functions to handle back button events
        componentDidMount()
        {
            this._blurGoingSubscribe = this.props.navigation.addListener('willBlur', payload =>
                    BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
                );
        }
    
        onBackButtonPressAndroid = () => {
            this.props.navigation.goBack();
            return true;
        };
    
        componentWillUnmount()
        {
            this._focusDoneSubscribe && this._focusDoneSubscribe.remove();
            this._blurGoingSubscribe && this._blurGoingSubscribe.remove();
        }
        //actual render
        render(){
            const { navigate } = this.props.navigation;
            return (
                
                    
                        Navigation BackHandler Tutorial
                        SCREEN ONE
                    
                
            );
        }
    }
    

提交回复
热议问题