fix(android): handle hardware back button and navigation with active panels/keyboard
Improve back navigation UX by checking for open panels or keyboard before leaving the chat screen. When back is triggered, dismiss keyboard or close panel first instead of immediately navigating away. Apply this logic to both the navigation gesture listener and Android hardware back button for consistent behavior.
This commit is contained in:
@@ -26,6 +26,7 @@ import {
|
||||
KeyboardAvoidingView,
|
||||
Platform,
|
||||
TouchableOpacity,
|
||||
BackHandler,
|
||||
} from 'react-native';
|
||||
import { FlashList, ListRenderItem } from '@shopify/flash-list';
|
||||
import { MaterialCommunityIcons } from '@expo/vector-icons';
|
||||
@@ -268,14 +269,44 @@ export const ChatScreen: React.FC<ChatScreenProps> = (props) => {
|
||||
});
|
||||
}, [displayMessages, flatListRef, setBrowsingHistory]);
|
||||
|
||||
// 监听返回事件,刷新会话列表
|
||||
// 监听返回事件:面板/键盘打开时先关闭它们,否则刷新会话列表后返回
|
||||
useEffect(() => {
|
||||
const unsubscribe = navigation.addListener('beforeRemove', () => {
|
||||
// 刷新会话列表,确保已读状态正确显示
|
||||
const unsubscribe = navigation.addListener('beforeRemove', (e) => {
|
||||
const hasPanelOpen = activePanel !== 'none';
|
||||
const hasKeyboardOpen = keyboardHeight > 0;
|
||||
if (hasPanelOpen || hasKeyboardOpen) {
|
||||
e.preventDefault();
|
||||
if (hasKeyboardOpen) {
|
||||
handleDismiss();
|
||||
} else {
|
||||
closePanel();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 真正返回时刷新会话列表
|
||||
messageManager.refreshConversations(true, 'chat-before-remove');
|
||||
});
|
||||
return unsubscribe;
|
||||
}, [navigation]);
|
||||
}, [navigation, activePanel, keyboardHeight, closePanel, handleDismiss]);
|
||||
|
||||
// Android 物理返回键:面板/键盘打开时先关闭它们
|
||||
useEffect(() => {
|
||||
const backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
|
||||
const hasPanelOpen = activePanel !== 'none';
|
||||
const hasKeyboardOpen = keyboardHeight > 0;
|
||||
if (hasPanelOpen || hasKeyboardOpen) {
|
||||
if (hasKeyboardOpen) {
|
||||
handleDismiss();
|
||||
} else {
|
||||
closePanel();
|
||||
}
|
||||
return true; // 阻止默认返回行为
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return () => backHandler.remove();
|
||||
}, [activePanel, keyboardHeight, closePanel, handleDismiss]);
|
||||
|
||||
// 渲染消息气泡
|
||||
const messageIndexMap = useMemo(() => {
|
||||
|
||||
Reference in New Issue
Block a user