是否有可能在onResume
中查找片段是否在片段事务或中,如果由于'回到应用程序而调用onResume
“吗
背景:
如果应用程序从另一个应用程序或后台返回,我想在恢复时触发网络调用,但如果片段刚刚创建并且在转换过程中设置了动画,我不想触发该调用(我正在执行此操作) onAnimationEnd
中动画的onCreateAnimation
以避免延迟动画
示例:
这是片段的基类。
abstract class AnimationSafeFragment : Fragment() {
abstract fun onReady()
override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation {
var animation = super.onCreateAnimation(transit, enter, nextAnim)
if (animation == null && nextAnim != 0) {
animation = AnimationUtils.loadAnimation(activity, nextAnim)
}
animation.let {
view?.setLayerType(View.LAYER_TYPE_HARDWARE, null)
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {}
override fun onAnimationEnd(p0: Animation?) {
view?.setLayerType(View.LAYER_TYPE_NONE, null)
onReady()
}
override fun onAnimationStart(p0: Animation?) {}
})
}
return animation
}
}
虽然我正在进行持久网络调用的每个片段都实现了这样的抽象类:
class FragmentX : AnimationSafeFragment() {
override fun onReady() {
fetchDataWithExpensiveCall()
updateUIExpensiveCall()
}
override fun onResume() {
super.onResume()
// call onReady here only if this is coming from background, not from an animation, because then we call it twice
if (conditionIamLookingFor) {
onReady()
}
}
}