【发布时间】:2012-02-08 21:06:29
【问题描述】:
我正在使用以下代码将片段插入到活动中:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getFragmentManager();
String tag = "simple";
Fragment fr = fm.findFragmentByTag(tag);
if (fr == null)
{
SimpleFragment simpleFragment = new SimpleFragment();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out,
android.R.animator.fade_in, android.R.animator.fade_out);
transaction.add(R.id.main_layout, simpleFragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
}
片段代码为:
public class SimpleFragment extends ListFragment
{
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getView().setBackgroundColor(Color.YELLOW);
}
}
当我在启动后通过返回按钮从后台弹出片段时,一切都很好,我可以看到淡出动画。但是,如果我旋转设备并按下后退按钮,那么片段就会消失而没有动画。
这是 Android 行为还是我做错了什么?
编辑: 旋转后 FragmentManager 似乎没有恢复 BackStackEntry 的动画(enterAnim、exitAnim、popEnterAnim 和 popExitAnim)。
FragmentManager 转储(无旋转):
Active Fragments in 4087d668:
#0: SimpleFragment{408883b0 #0 id=0x7f050000 simple}
mFragmentId=#7f050000 mContainerId#=7f050000 mTag=simple
mState=4 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
mHidden=false mDetached=false mRetainInstance=false mRetaining=false mHasMenu=false
mFragmentManager=FragmentManager{4087d668 in ListViewFragmentsActivity{4087d588}}
mImmediateActivity=my.app.ListViewFragmentsActivity@4087d588
mActivity=my.app.ListViewFragmentsActivity@4087d588
mNextAnim=17498112
mContainer=android.widget.RelativeLayout@408876d8
mView=android.widget.FrameLayout@40888a70
Added Fragments:
#0: SimpleFragment{408883b0 #0 id=0x7f050000 simple}
Back Stack:
#0: android.app.BackStackRecord@408884b8
mName=simple mIndex=0 mCommitted=true
mEnterAnim=#10b0000 mExitAnim=#10b0001
Operations:
Op #0:
cmd=1 fragment=SimpleFragment{408883b0 #0 id=0x7f050000 simple}
enterAnim=17498112 exitAnim=17498113
popEnterAnim=17498112 popExitAnim=17498113
Back Stack Indices:
#0: android.app.BackStackRecord@408884b8
FragmentManager misc state:
mCurState=5 mStateSaved=false mDestroyed=false
FragmentManager 转储(旋转后):
Active Fragments in 40877f38:
#0: SimpleFragment{40878858 #0 id=0x7f050000 simple}
mFragmentId=#7f050000 mContainerId#=7f050000 mTag=simple
mState=4 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
mHidden=false mDetached=false mRetainInstance=false mRetaining=false mHasMenu=false
mFragmentManager=FragmentManager{40877f38 in ListViewFragmentsActivity{40877e58}}
mImmediateActivity=my.app.ListViewFragmentsActivity@40877e58
mActivity=my.app.ListViewFragmentsActivity@40877e58
mContainer=android.widget.RelativeLayout@4087ed50
mView=android.widget.FrameLayout@4087fc00
Added Fragments:
#0: SimpleFragment{40878858 #0 id=0x7f050000 simple}
Back Stack:
#0: android.app.BackStackRecord@40878a78
mName=simple mIndex=0 mCommitted=false
Operations:
Op #0:
cmd=1 fragment=SimpleFragment{40878858 #0 id=0x7f050000 simple}
Back Stack Indices:
#0: android.app.BackStackRecord@40878a78
FragmentManager misc state:
mCurState=5 mStateSaved=false mDestroyed=false
【问题讨论】:
-
我也遇到了同样的问题。你有没有运气弄明白?
-
@khendricks 不,你呢?
-
很遗憾没有。我很震惊,没有很多其他人抱怨这一点。我为此问题创建了一个错误报告。 code.google.com/p/android/issues/…
-
@khendricks,试试 Android api 提供的 Support4Demos 示例项目。在运行时你可以观察/检查 Fragment->Custom Animation,在代码中观察文件 FragmentCustomAnimationSupport.java。即使您更改方向应用程序也不会崩溃,甚至动画也可以正常工作。请让我知道它是否适合你。
-
@khendricks 不,它显示了他所描述的确切行为。当你前进时它工作正常,但是当方向改变后返回时它不显示动画。它对我来说从来不是问题的原因是我们总是将方向设置为纵向或横向。
标签: android animation fragment