Transaction BackStack and its management

Posted by Achin | Filed under Android
19.09.2014

This is second part of a 6 posts series. In the first post I talked around basics of fragment oriented architecture. From this post onwards, I’ll be talking about it’s implementation details.

(Sample application’s source code and README)

In this part I am going to talk about Transaction Backstack and few related methods that can be used frequently.
Transaction BackStack has often been misinterpreted as backstack of fragments. FragmentManager inside an activity deals with fragment-transactions rather than with fragments. An entry into this backstack is a ‘fragment-transaction’ which can be composed of one or more operations involving fragment(s). Reverting this would revert back all these operations together.

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(restId, fragmentA);
ft.replace(fragmentB);
ft.commit();

Above, is a single transaction clubbing multiple operations.

Transactions do not get added to back stack by default. An explicit call toaddToBackStack(String tag) before committing it, would add it to BackStack. The ‘tag’ string would be an identifier of the transaction and can be used to refer back to it later.

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(resId, fragmentA);
ft.replace(resId, fragmentB);
ft.addToBackstack("tag");
ft.commit();

Reverse of this method is popBackStack() and its following variants.

  • popBackStack() pops the top transaction from stack.
  • popBackStack(String tag) pops transactions until the transaction with supplied ‘tag’.
  • popBackStack(String tag, int flag) pops till the transaction with mentioned ‘tag’ if flag is POP_BACK_STACK_INCLUSIVE, or else 0 can be used.
  • popBackStack(int id, int flag) pops till the transaction with mentioned ‘tag’. Id is the identifier returned from FragmentTransaction.commit().

Above methods actually just enqueues a transaction in FragmentManager’s ToDo queue and would come in effect only when application returns to its event loop. In order to enforce the transactions to come in effect immediately, there are following additional variants with same effects respectively.

  • popBackStackImmediate()
  • popBackStackImmediate(String tag)
  • popBackStackImmediate(String tag, int flag)
  • popBackStackImmediate(int id, int flag)

How transaction backstack is not fragment backstack, is demonstrated in the sample app in ‘Back Stack Handler’ section as follows. If you press ‘Go to step 1’ button, the transaction adds  FirstStepFragment and commits. Thus, on popBackStack() from this fragment, app would remove FirstStepFragment and get back to ‘BackStackHandlerFragment’.

// TO ADD A FRAGMENT.
@Override
public void addFragment(BaseFragment baseFragment, boolean withAnimation) {      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();if(withAnimation) {   ft.setCustomAnimations(R.anim.fragment_slide_in_left, R.anim.fragment_slide_out_left, R.anim.fragment_slide_in_right, R.anim.fragment_slide_out_right);    }ft.replace(R.id.home_frame_layout_for_fragments, baseFragment, baseFragment.getTagText());ft.addToBackStack(baseFragment.getTagText());ft.commit();
}

Instead, if you press ‘Go to step2’ in BackStackHandlerFragment, a transaction would occur with two ‘add’ operations, adding both FirstStepFragment and SecondStepFragment clubbed in a single transaction. So, a single popBackStack() call from SecondStepFragment would remove both these fragments and app would get back to BackStackHandlerFragment.

// TO ADD MULTIPLE FRAGMENTS CLUBBED INTO ONE TRANSACTION.@Override
public void addMultipleFragments(BaseFragment[] baseFragments) {// Initialize a Fragment Transaction.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();// Record all steps for the transaction.for(int i = 0 ; i < baseFragments.length ; i++) {ft.setCustomAnimations(R.anim.fragment_slide_in_left, R.anim.fragment_slide_out_left, R.anim.fragment_slide_in_right, R.anim.fragment_slide_out_right);ft.replace(R.id.home_frame_layout_for_fragments, baseFragments[i], baseFragments[i].getTagText());}// Add the transaction to backStack with tag of first added fragmentft.addToBackStack(baseFragments[0].getTagText());// Commit the transaction.ft.commit();
}

Another requirement could be of clearing the back stack.

// CLEAR BACK STACK.
private void clearBackStack() {final FragmentManager fragmentManager = getSupportFragmentManager();while (fragmentManager.getBackStackEntryCount() != 0) {fragmentManager.popBackStackImmediate();}
}

The above method loops over all the transactions in the backstack and removes them immediately one at a time.

You may want to take a look at the following as well while dealing with transaction backstack management.

  • OnBackStackChangedListener
  • FragmentManager.getbackStackEntry(int index)

关于fragment backstate的介绍相关推荐

  1. Fragment的详细介绍和使用方法

    [Android UI设计与开发]第07期:底部菜单栏(二)Fragment的详细介绍和使用方法 标签: androidFragment界面设计底部菜单栏Wi-Fi 2013-05-31 22:38  ...

  2. 【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8995025 由于TabActivity在Android4.0以后已经被完全弃 ...

  3. 底部菜单栏之Fragment的详细介绍和使用方法

    由于TabActivity在Android4.0以后已经被完全弃用,那么我就不再浪费口水继续讲解它了,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻 ...

  4. 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法...

    原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...

  5. Android之Fragment的详细介绍和使用方法

    一.Fragment的基础知识介绍 1.1概述 1.1.1 特性 Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面 ...

  6. 关于fragment backState的原理

    在使用Fragment的时候我们一般会这样写: FragmentTransaction transaction = getSupportFragmentManager().beginTransacti ...

  7. 关于fragment backstate的运用

    这两天在使用Fragment做播放器里的播放列表和歌词显示两个界面的替换与更新时发现了很多问题,在此记录下问题及解决方法: 1.多个Fragment在replace后(并且都加入了后退栈ft.addT ...

  8. Android开发帮助技巧(适用于入门)(第一部分-高效地构建项目的准备工作和Activity与Fragment的交互介绍)

    平台: windows11 Android Studio 4.2.2 Build #AI-202.7660.26.42.7486908, built on June 24, 2021 Runtime ...

  9. Android Fragment(三)ListFragment简单介绍以及Fragment之间通信

    一.Fragment通信简单介绍:Fragments之间是不能够直接通信的,他们之间的通信是通过Activity这个中间件来通信的, 为了让Fragment跟它的Activity通信,我们可以在Fra ...

最新文章

  1. Linux磁盘空间被占满?清空回收站试试!
  2. html和ascll有什么关联,什么是HTML ASCII(HTML ASCII)?
  3. applicationcontext and webapplicationcontext
  4. 作业MathExam
  5. 94. Ext.MessageBox消息框
  6. aws s3 獲取所有文件_Url从Amazon S3获取文件
  7. 一些有趣的 CSS 魔法和布局(下)(结尾有岗位内推哦~)
  8. OpenLayers 在Vue中增删改
  9. Training_model(2)
  10. 基于MATLAB的列车防护曲线组合步长算法分析与仿真验证
  11. 解决:蓝奏云下载链接没法打开问题
  12. Unity-使用UPR资源检测工具AssetChecker-Win进行本地资源检测
  13. python背景颜色代码大全_python3中布局背景颜色代码分析
  14. 京东区块链开源项目——JD Chain介绍及区块链白皮书发布
  15. C语言核心知识点大汇总
  16. 运维36讲第07课:基于 Django_crontab、Xadmin 做一套定时任务管理系统
  17. 深度学习CTPN+CRNN模型实现图片内文字的定位与识别(OCR)
  18. 吉林大学微型计算机试卷,微机原理及应用 吉林大学考试题库答案
  19. 深圳市大数据研究院(医疗大数据实验室)招聘博士/硕士/博士后/科研助理
  20. MTPuTTY报错:Unable to run PuTTY 系统找不到指定的文件

热门文章

  1. 复选框 全选 全不选 反选 实现
  2. GitHub之GitHub Actions的项目自动化持续集成和部署
  3. 126. Word Ladder II
  4. Python如何防止sql注入
  5. Linux停止后台运行Django项目
  6. 【Linux】一步一步学Linux——dpkg-reconfigure命令(272)
  7. 【Linux】一步一步学Linux——crontab命令(132)
  8. linux 查看正在执行的进程的pid编号_不小心执行 rm f,该如何恢复?
  9. activexobject对象不能创建_面向对象设计方法(Object oriented)
  10. 字符串所有排列组合暴力递归