[转]使用popBackStack()清除Fragment
![](http://static.adzerk.net/Advertisers/af217662e49a4cbda030feae88418cdd.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjozMjI1MTQsImNoIjoxMTc4LCJjayI6e30sImNyIjoxMTk1NzgwLCJkaSI6Ijc5ZjhiOGQ2M2QzOTQ5OGFiYTgzZWVjYTQ3MjhiNGM0IiwiZG0iOjEsImZjIjoxMjY5MTM5LCJmbCI6ODY0MjI1LCJpcCI6IjU4LjQ5LjI0OC4xMzgiLCJrdyI6ImFuZHJvaWQsYW5kcm9pZC1mcmFnbWVudHMiLCJudyI6MjIsInBjIjowLCJlYyI6MCwicHIiOjE2MDQsInJ0IjoxLCJyZiI6Imh0dHA6Ly93d3cueHVlYnV5dWFuLmNvbS8xNTQ3NTQ4Lmh0bWwiLCJzdCI6ODI3NywidWsiOiJ1ZTEtY2VkM2UwMDYyOTY5NDc4MmExODU5NTM3MjQ4Y2I5YTkiLCJ6biI6NDMsInRzIjoxNDQ2NDAxNTgwNzAzLCJiZiI6dHJ1ZSwicG4iOiJhZHplcms2ODA1NDI2ODgifQ&s=SkFlh_meVwbGNnd2NeQAvHAcV3E)
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 ...
随机推荐
- struts集合类型封装
1.list类型封装
- php中的转义函数
<?php parse_url 解析URL, 返回各组成部分 urlencode/urldecode url编码/解码 htmlentities 将字符串转化为html实体 htmlentiti ...
- screen命令使用方法【转】
在linux的环境中,我们想要在后台持续运行一些脚本,但是又因为关闭这个tty的话,脚本就会中断,这个时候我们就需要screen这个工具的帮助啦! 基础 1 首先先查看下否则有这个工具.如果运行s ...
- LGPL 与GPL的区别
GPL(GNU General Public License) 我们很熟悉的Linux就是采用了GPL.GPL协议和BSD, Apache Licence等鼓励代码重用的许可很不一样.GPL的出发点是 ...
- 5.rabbitmq 主题
1.生产者 #!/usr/bin/env python import pika import sys connection = pika.BlockingConnection(pika.Connect ...
- Codeforces 799B - T-shirt buying(STL)
题目链接:http://codeforces.com/problemset/problem/799/B 题目大意:有n件T恤,每件T体恤都分别有价格(每件衣服的价格不重复).前面的颜色.背部的颜色三种 ...
- 以太坊go-ethereum客户端JSON-RPC API调用(一)
前几篇博客主要介绍了go-ethereum客户端不同环境的搭建,今天这篇博客是建立在前几排博客的基础上.当搭建完成之后,我们可以通过各种方式与节点进行交互(JavaScript Console.JSO ...
- web 端 gantt组件选型
gantt - 甘特图 甘特图(Gantt chart)又称为横道图.条状图(Bar chart).其通过条状图来显示项目,进度,和其他时间相关的系统进展的内在关系随着时间进展的情况.以提出者亨利·L ...
- Fedora下rstudio-server安装
安装 sudo yum install openssl sudo yum install mysql-server wget https://download2.rstudio.org/rstudio ...
- Web前端开发最佳实践(3):前端代码和资源的压缩与合并
一般在网站发布时,会压缩前端HTML.CSS.JavaScript代码及用到的资源文件(主要是图片文件),目的是加快文件在网络中的传输,让网页更快的展现.当然,CDN分发.缓存等方式也是加快代码或资源 ...