RN—Android 物理返回鍵監聽


A → B

使用 navigator 導航,用 goBack() 返回的時候傳遞參數

A 頁面

this.props.navigation.navigate("B", { callBack: (data) => {       //回調函數
                                this.setState({ studentCount: data }) } });

B 頁面

  onPress()=>{ this.props.navigation.state.params.callBack(data) this.props.navigation.goBack(); }

在使用 Android 機的時候,點擊物理返回鍵的時候,並不會走 navigator ,所以需要加一個返回鍵的事件監聽

1.在生命周期中監聽

componentDidMount() { BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid); } componentWillUnmount() { BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid); } onBackAndroid = () => { this.props.navigation.goBack(); this.props.navigation.state.params.callBack(data) return true }

2.待測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM