Fragment Transactions Reference/Animations

I recently found myself looking at Fragment transactions and wondering precisely what each Fragment state meant.  I vaguely had an idea of what added, attached, shown, hidden, detached and removed meant, but not the specific behaviors of each.  I spent a morning looking through Android‘s code to better understand what happens to Fragments.

Here‘s a reference manual for what I learned.  I looked into two aspects of Fragments: fragment states, and animations in transactions.  This research was done primarily on the compatibility library.  It‘s possible that some of the details below will differ for the 3.x+ implementation of Fragments.

Fragment States

All of the below support animations.  Terminology note: when I say "configuration change", I mean when the device changes configuration (for example, from portrait to landscape) and the Activity must be destroyed/recreated.

FragmentTransaction.add()/remove() - These add and remove a Fragment from the FragmentManager.  By default, it will begin showing the Fragment as well.  During the transaction the returns of Fragment.isAdded() and Fragment.isRemoved() will be modified.  These states are preserved on configuration change, so a Fragment that‘s added will remain added and can be retrieved again from the FragmentManager.  Since a removed Fragment is no longer part of the FragmentManager, it will not be retrievable after a configuration change either.

FragmentTransaction.attach()/detach() - These attach/detach a Fragment from the Activity (e.g., make it an active Fragment that can be seen).  During the transaction the returns of Fragment.isAdded() and Fragment.isDetached() will be changed.  These states are preserved on configuration change, which means that (unlike removing a Fragment) it is possible to detach a Fragment but then retrieve it later after configuration change (even if it‘s not actively being displayed).  Even in a detached state, the Fragment runs through all lifecycle methods (except the ones that are related to being attached to an Activity) - for example, onSaveInstanceState() will still be called even if the Fragment is detached (which is quite useful for maintaining state).

FragmentTransaction.show()/hide() - These show or hide a Fragment (in particular, it calls View.setVisibility() on the Fragment‘s root View).  During the transaction the returns of Fragment.isHidden() will be modified.  Showing/hiding Fragments states are NOT maintained on configuration change, meaning that if you hide a Fragment it will reappear on config change (for example, if the user rotates the screen).

FragmentTransaction.replace() - Theoretically it is the exact same as removing all Fragments associated with a particular View, then adding a new Fragment in its place.  I did not research this much (since I‘m more interested in manual control of which Fragments are shown).

Fragment Animations

There are three different ways to animate Fragment transactions.  These are ordered by precedence (in other words, the first method, if available, will be used; then the second, then the last).

One thing to note between the compatibility library and the standard Fragment library is that the standard library uses property animators, whereas the compatibility library uses tween Animations.

1. Override your Fragment‘s onCreateAnimator() method and return an animation.  This is the only way to dynamically generate an animation in code (the other two methods rely on pre-configured animations from your resources directory).  In the compatibility library, the method is onCreateAnimation() (since the compatibility library uses animations instead of animators).

2. Call FragmentTransaction.setCustomAnimations(), referencing either animators or animations (depending on whether you‘re using the compatibility library or not).  What is interesting is that setCustomAnimations() affects all fragment transitions added to the transaction after it is called.  So you need to call setCustomAnimations() before you want it used, and you can actually setup multiple different custom animations for each part of a transaction (with a call to setCustomAnimations() before each add()/remove()/attach()/detach()/show()/hide()/replace()).

3. Setup a style that defines window animations (from this style), then callFragmentTransaction.setTransition() and FragmentTransaction.setTransitionStyle() to specify that style and target the correct animation.

#3 works different in the compatibility library.  FragmentTransaction.setTransitionStyle() is completely ignored.  Instead, it uses the specified transition to select a stock animation.  So if you want custom animations in the compatibility library, you must use #1 or #2.

Via: http://daniel-codes.blogspot.com/2012/06/fragment-transactions-reference.html

时间: 2024-10-29 03:53:10

Fragment Transactions Reference/Animations的相关文章

Fragment Transactions和Activity状态丢失

本文由 伯乐在线 - 独孤昊天 翻译.未经许可,禁止转载!英文出处:androiddesignpatterns.欢迎加入翻译组. 下面的堆栈跟踪和异常代码,自从Honeycomb的初始发行版本就一直使得StackOverflow很迷惑. 1 2 3 4 5 java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState     at android.support.v4.app.Fragm

Fragment Transactions & Activity State Loss

原文地址:http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html 作者:Alex Lockwood 3013年8月20日 Honeycomb首版发布以来,以下堆栈跟踪和异常消息就一直困扰着StackOverflow <span style="font-size:18px;">java.lang.IllegalStateException: Can not

Android Studio--ViewPager的使用(未使用fragment)

前言:这里只是对 ViewPager的一个简单的使用介绍,参考自博客http://blog.csdn.net/harvic880925/article/details/38453725 . 一个小例子 1.向注布局中添加 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

Fragment基础操作

Fragment和Activity类似,同样是具备UI的属性:也就是都能用于规划UI布局... Building a Dynamic UI with Fragments --> Fragments具备有动态UI的属性.为了在Android上为用户提供动态的.多窗口的交互体验,我们需要将UI组件和Activity操作封装成模块进行使用,使得我们可以在activity中对这些模块进行切入切出操作.可以用Fragment来创建这些模块,Fragment就像一个嵌套的activity,拥有自己的布局(l

Android学习路线(二十二)运用Fragment构建动态UI——构建一个灵活的UI

先占个位置,下次翻译 :p When designing your application to support a wide range of screen sizes, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset devi

Android Fragment使用(一) 基础篇 温故知新

Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, id, 如果都没有, container的id将会被使用. 方法二: 动态添加 动态添加利用了一个transaction: FragmentManager fragmentManager = getFragmentManager(); Fragment fragment = fragmentMan

Square:从今天开始抛弃Fragment吧!

原文链接 : Advocating Against Android Fragments 原文作者 : Pierre-Yves Ricau 译文出自 : 开发技术前线 www.devtf.cn 译者 : chaossss 校对者: 这里校对者的github用户名 状态 : 完成 最近我在 Droidcon Paris 上进行了 一个技术相关的演讲 ,我在这次演讲中给大家展示了 Square 使用 Fragment 进行开发时遇到的种种问题,以及其他 Android 开发者是怎么避免在项目中使用 F

管理Fragments

FragmentManager 为了管理Activity中的fragments,需要使用FragmentManager. 为了得到它,需要调用Activity中的getFragmentManager()方法. 因为FragmentManager的API是在Android 3.0,也即API level 11开始引入的,所以对于之前的版本,需要使用support library中的FragmentActivity,并且使用getSupportFragmentManager()方法. 用Fragme

Android设计和开发系列第二篇:Action Bar(Develop—API Guides)

Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an icon Adding Action Items Handling clicks on action items Using split action bar Navigating Up with the App Icon Adding an Action View Handling collap