求android老手指导。。

dai_jiawei 2014-09-30 11:03:27
我在一个Acitivity里面写了多个Fragment.。有好几层的fragment在跳转,但是我一按手机上的回退键就把当前的Activity关掉了,也就是fragment全部都被关掉了。我想实现当按回退的时候返回到上一个fragment。。请问能否实现?该如何实现?
...全文
166 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
罗博士 2014-10-05
  • 打赏
  • 举报
回复
支持3楼和5楼。
Kifile 2014-10-03
  • 打赏
  • 举报
回复
引用 4 楼 dai_jiawei 的回复:
引用 3 楼 DamonZH 的回复:
Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.
我自己写了一个回退的按钮,那我要怎样让我点击它的时候它也是返回上一个fragment而不是把activity finish掉呢
获取 Fragment 的 Trasaton, 然后调用他的弹出函数,具体函数你可以查一下
Kifile 2014-10-03
  • 打赏
  • 举报
回复
何必能, fragment 原生就支持返回逻辑的,只要你在 commit()之前调用 addBackStack(),如果没有记错,是这一个函数吧,
dai_jiawei 2014-10-03
  • 打赏
  • 举报
回复
引用 3 楼 DamonZH 的回复:
Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.
我自己写了一个回退的按钮,那我要怎样让我点击它的时候它也是返回上一个fragment而不是把activity finish掉呢
DamonZH 2014-10-01
  • 打赏
  • 举报
回复
Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.
西兰花不蓝 2014-10-01
  • 打赏
  • 举报
回复
重写返回键的回调方法不行么?
svenwang 2014-09-30
  • 打赏
  • 举报
回复
能。 让每个fragment对应不同的activity。

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧