android 动画反向旋转,android – setRetainInstance(true)setCustomAnimations(…)=每个方向更改的动画?...

在Android应用中,遇到一个关于片段(Fragment)和配置变更的问题。当创建片段时,需要添加动画,但在屏幕方向改变时,不希望重新播放动画。使用`setRetainInstance(true)`来避免片段在配置变更时被重建,但动画仍然会在旋转设备时重启。尝试过使用`replace`代替`add`以及检查片段是否存在时避免动画,但未解决问题。解决方案包括在配置变更时快速移除并重新添加片段以避免动画,或者在片段视图创建和销毁时直接处理动画。这两种方法都可能导致额外的生命周期问题或不期望的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

我有一个活动,片段需要在创建时进行动画处理,但在方向改变时则不需要.

片段被动态插入到布局中,因为它是导航抽屉式活动的一部分.

问题

我想避免为配置更改重新创建片段,所以我在片段中使用了setRetainInstance.

它可以工作,但由于某种原因,每次旋转设备时动画也会重新启动.

我做了什么

我已将此添加到片段中:

@Override

public void onCreate(final Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setRetainInstance(true);

}

这对活动:

final FragmentManager fragmentManager = getSupportFragmentManager();

final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

MyFragment fragment= (MyFragment) fragmentManager.findFragmentByTag(MyFragment.TAG);

if (fragment== null) {

fragmentTransaction.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);

fragment= new MyFragment();

fragmentTransaction

.add(R.id.fragmentContainer, fragment, MyFragment.TAG).commit();

}

fragment_container.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/fragmentContainer"

android:layout_width="match_parent"

android:layout_height="match_parent" />

我试过的

>我试图通过使用“替换”而不是“添加”来修复它.它没有帮助.

>我也尝试过总是执行片段的替换,如果片段已经存在,那就不用动画(在同一个片段上).

>如果我删除setRetainInstance调用,它可以工作,但我想避免重新创建片段.

>我该如何解决这个问题?

>为什么我仍然可以获得添加片段的动画?

>其他配置发生变化时会发生什么?

解决方法#1

这个解决方案通常有效,但它会给你试图实现的生命周期带来不好的结果:

MyFragment fragment= (MyFragment) fragmentManager.findFragmentByTag(MyFragment.TAG);

if (MyFragment== null) {

MyFragment= new MyFragment();

fragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right)

.replace(R.id.fragmentContainer, fragment, MyFragment.TAG).commit();

} else {

//workaround: fragment already exists, so avoid re-animating it by quickly removing and re-adding it:

fragmentManager.beginTransaction().remove(fragment).commit();

final Fragment finalFragment = fragment;

new Handler().post(new Runnable() {

@Override

public void run() {

fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment, finalFragment .TAG).commit();

}

});

}

我仍然希望看到可以做什么,因为这会导致你不想发生的事情(例如onDetach for the fragment).

解决方法#2

解决此问题的一种方法是避免通过fragmentManager添加动画,并在片段生命周期内为视图本身执行此操作.

这是它的样子:

BaseFragment

@Override

public void onViewCreated(final View rootView, final Bundle savedInstanceState) {

super.onViewCreated(rootView, savedInstanceState);

if (savedInstanceState == null)

rootView.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_from_left));

}

@Override

public void onDestroyView() {

super.onDestroyView();

if (!getActivity().isChangingConfigurations())

getView().startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值