Android RN 从native页面返回到RN页面时,Alert框无法弹出
问题描述:
从native页面返回到RN页面,RN页面通过在componentWillMount 中注册native监听bridge.addNativeListener(‘EVENT_CHANGE_TOURIST’,(data) => {…}, 在监听中执行逻辑,弹出Alert框。
参考场景如:选择出游人,跳转到native常旅页面去选择出游人,把选择好的出游人带入到预定页RN页面,如果旅客信息不满足条件,则Alert提示。
Alert未弹出
从native页面返回时,RN页面可能不在前台,此时执行Alert代码,不会弹出。
在执行RN代码Debug时,弹出了Alert;原因分析,在debug时RN页面已回到前台,再执行Alert逻辑,可以弹出。
解决方案
通过延时,等待RN页面回到前台,再执行Alert.
参考代码:
componentWillUnmount() {
this.travelerListener && bridge.removeNativeListener(this.travelerListener);
this.timer && clearTimeout(this.timer);
}
...
在需要弹出Alert的地方,执行 showAlert(msg)
/**
* 弹出框
* @param msg
*/
showAlert(msg){
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(() => {
Alert.alert('', msg, [{text: '确定'}]);
}, 10);
}