[转]使用popBackStack()清除Fragment
I ported my Android app to honeycomb and I did a big refactor in order to use fragments. In my previous version, when I pressed the Home button I used to do a ACTIVITY_CLEAR_TOP in order to reset the back stack. Now my app is just a single Activity with multiple fragmens, so when I press the Home button I just replace one of the fragments inside it. How can I clear my back stack without having to use startActivity with the ACTIVITY_CLEAR_TOP flag? |
|||||||||
|
I posted something similar here From Joachim's answer, from Dianne Hackborn: http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42 I ended up just using:
But could equally have used something like:
Which will pop all states up to the named one. You can then just replace the fragment with what you want |
|||||||||||||||||||||
|
To make an answer for Warpzit comment and make it easiere to find for others, use:
|
|||||||||
|
With all due respect to all involved parties; I'm very surprised to see how many of you could clear the entire fragment back stack with a simple
According to Android documentation (regarding the
Now, I do realize that I'm lacking knowledge of your particular implementations (like how many entries you have in the back stack at the given point in time), but I would bet all my money on the accepted answer when expecting a well defined behaviour over a wider range of devices and vendors: (for reference, something along with this)
|
|||||
|
Accepted answer was not enough for me. I had to use :
|
|||
I just wanted to add :-- Popping out from backstack using following
is just about removing the fragments from the transaction, no way it is going to remove the fragment from the screen. So ideally, it may not be visible to you but there may be two or three fragments stacked over each other, and on back key press the UI may look cluttered,stacked. Just taking a simple example:- Suppose you have a fragmentA which loads Fragmnet B using fragmentmanager.replace() and then we do addToBackStack, to save this transaction. So the flow is :-- STEP 1 -> FragmentA->FragmentB (we moved to FragmentB, but Fragment A is in background, not visible). Now You do some work in fragmentB and press the Save button—which after saving should go back to fragmentA. STEP 2-> On save of FragmentB, we go back to FragmentA. STEP 3 ->So common mistake would be... in Fragment B,we will do fragment Manager.replace() fragmentB with fragmentA. But what actually is happenening, we are loading Fragment A again, replacing FragmentB . So now there are two FragmentA (one from STEP-1, and one from this STEP-3). Two instances of FragmentsA are stacked over each other, which may not be visible , but it is there. So even if we do clear the backstack by above methods, the transaction is cleared but not the actual fragments. So ideally in such a particular case, on press of save button you simply need to go back to fragmentA by simply doing fm.popBackStack() or fm.popBackImmediate(). So correct Step3-> fm.popBackStack() go back to fragmentA, which is already in memory. |
|||
Clear backstack without loops
Where name is the addToBackStack() parameter
|
|||
I got this working this way:
|
|||
Works for me and easy way without using loop:
|
|||
[转]使用popBackStack()清除Fragment的更多相关文章
- Android Fragment 解析和使用
Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...
- Fragment响应返回键
Activty可以直接响应返回键,而Fragment却不行,可用如下方式: 创建一个抽象类BackHandledFragment,该类中有一个抽象方法onBackPress(),所有BackHandl ...
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
- 让Fragment监听返回键
Activity可以很容易的得到物理返回键的监听事件,而Fragment却不能.所以使用到了以下的方法. 首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPre ...
- Fragment 总结
本博客代码地址 : -- 单一 Fragment 示例 : https://github.com/han1202012/Octopus-Fragement.git -- 可复用的 Fragment 示 ...
- Android开发之Fragment的介绍、使用及生命周期
Fragment官网介绍-http://developer.android.com/guide/components/fragments.html 郭大神的使用实例文章:http://blog.csd ...
- Android学习笔记(五)Fragment简介
Fragment是在Android 3.0 (API level 11)中引入的Activity的子模块.初衷是为了适应大屏幕的平板电脑,我们只需要使用Fragment对UI组件进行分组.模块化管理, ...
- 【Android 应用程序开发】 Fragment 详细说明
笔者 : 汉书亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragmen ...
- 【Android 应用开发】 Fragment 详解
作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragmen ...
随机推荐
- flask插件系列之Flask-WTF表单
flask_wtf是flask框架的表单验证模块,可以很方便生成表单,也可以当做json数据交互的验证工具,支持热插拔. 安装 pip install Flask-WTF Flask-WTF其实是对w ...
- BAT-快速切换JDK1.6/1.7
Code: @echo OFF rem 修改背景/字体颜色为黑/绿 color 0A rem 自动设置JDK环境变量,本程序不会对 [系统环境变量] 造成破环!不需要重启!! rem 没有%%JAVA ...
- __inet_insert_ifa/__inet_del_ifa
/* 添加ip地址 主地址添加到最后一个满足范围的主地址后面 从地址添加到整个列表后面 若列表中存在与插入地址在同一子网的地址,则 要求ip地址不同且范围相同,并且插入地址认为是从地址 */ stat ...
- 继电器是如何成为CPU的(1)【转】
转自:http://www.cnblogs.com/bitzhuwei/p/from_relay_to_tiny_CPU.html 阅读目录(Content) 从电池.开关和继电器开始 用继电器做个与 ...
- WebClient vs HttpClient vs HttpWebRequest
转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/ Just when I was startin ...
- STL中heap相关函数
heap并不是属于STL中的containers,而是在<algorithm>下提供了相关的函数 make_heap,sort_heap,pop_heap,push_heap 函数的说明: ...
- Linux /etc/cron.d作用(转自 定时任务crontab cron.d)
原文链接:http://huangfuligang.blog.51cto.com/9181639/1608549 一.cron.d增加定时任务 当我们要增加全局性的计划任务时,一种方式是直接修改/et ...
- 使用FormData数据做图片上传: new FormData() canvas实现图片压缩
使用FormData数据做图片上传: new FormData() canvas实现图片压缩 ps: 千万要使用append不要用set 苹果ios有兼容问题导致数据获取不到,需要后台 ...
- SSD回归类物体检测
本宝宝最近心情不会,反正这篇也是搬用别人博客的了:(SSD就是YOLO+anchor(不同feature map 作为input)) 引言 这篇文章是在YOLO[1]之后的一篇文章,这篇文章目前是一篇 ...
- 连接数据库:ERROR:The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration prop
本打算在maven项目中配置mybatis试试看,想到mybatis如果不是在容器中运行,那么他的事务控制实际上可以使用的是jdbc的提交和回滚,这就要在pom.xml文件中配置mysql-conne ...