diff --git a/README.md b/README.md index 8ace531..dceaa97 100644 --- a/README.md +++ b/README.md @@ -30,5 +30,5 @@ - [ ] 支持显示回复图片 - [ ] 在回复中显示头像 - [x] 当滑动到页面底部时显示"加载更多" -- [ ] 支持通过 Android BackHandler 从话题详情返回话题列表 +- [x] 支持通过 Android BackHandler 从话题详情返回话题列表 diff --git a/src/ui/YooForumUI.js b/src/ui/YooForumUI.js index a5cf94f..86b4099 100644 --- a/src/ui/YooForumUI.js +++ b/src/ui/YooForumUI.js @@ -9,6 +9,7 @@ import { Animated, Dimensions, Pressable, + BackHandler, } from 'react-native'; import YooForumTopic from '../component/YooForumTopic'; import YooReply from './YooReply'; @@ -88,9 +89,24 @@ export default class YooForumUI extends Component { this.replyRef = createRef(); this.showDetail = this.showDetail.bind(this); this.hideDetail = this.hideDetail.bind(this); + this.handleAndroidBack = this.handleAndroidBack.bind(this); this.currentForumTopicUI = null; } + componentDidMount() { + this.backHandler = BackHandler.addEventListener( + 'hardwareBackPress', + this.handleAndroidBack, + ); + } + + handleAndroidBack() { + if (this.currentForumTopicUI){ + this.hideDetail(); + return true; + } + } + showDetail(ForumTopicUI) { this.currentForumTopicUI = ForumTopicUI; this.replyRef.current.scrollTo({x: 0, y: 0, animated: false});