防止片段自定义动画在屏幕旋转上播放?
问题描述:
我在FragmentTransaction中为我的秒表片段使用自定义动画。下面的代码:防止片段自定义动画在屏幕旋转上播放?
private void addStopWatch() {
if(_stopwatchFragment == null) {
_stopwatchFragment = new StopwatchFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.anim_slide_down, R.anim.anim_slide_up)
.add(R.id.ContentContainer, _stopwatchFragment, STOPWATCH_TAG)
.commit();
_stopwatchVisible = true;
}
}
当屏幕旋转时,R.anim.anim_slide_down动画会再次播放(在这里我不添加新片段,我重新连接已经存在的一个) 。有没有办法避免这种行为,只让片段与活动视图一起出现?
答
这可以很容易地通过在您的活动中使用静态字段来解决。虽然我不知道这需要适当的清理上的活动破坏
private void addStopWatch() {
if(_stopwatchFragment == null) {
_stopwatchFragment = new StopwatchFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(!hasAnimated) {
fragmentTransaction.setCustomAnimations(R.anim.anim_slide_down, R.anim.anim_slide_up)
.add(R.id.ContentContainer, _stopwatchFragment, STOPWATCH_TAG)
.commit();
hasAnimated = true
} else {
fragmentTransaction.add(R.id.ContentContainer, _stopwatchFragment, STOPWATCH_TAG)
.commit();
}
_stopwatchVisible = true;
}
}
:这个声明为您的活动类的静态成员:
private static boolean hasAnimated = false;
然后你的方法里,你可以做到这一点。可以肯定的是,在onDestroy()中设置hasAnimated为false。
简单而优雅的解决方案,我希望我想到了它:)谢谢。 – 2012-07-31 19:14:16