android / animation-samples

Multiple samples showing the best practices in animation on Android.
Apache License 2.0
2.56k stars 898 forks source link

Crash in sample DrawableAnimations on exiting AnimatedVectorDrawableCompat #61

Closed amazingvoice closed 2 years ago

amazingvoice commented 2 years ago

Steps to observe this issue: start the app DrawableAnimations -> click AnimatedVectorDrawableCompat -> click START -> back press before the animation is done

crash stacktrace: java.lang.IllegalStateException: Fragment AnimatedFragment{50ea0d2} (32e3cce7-653f-446c-8e11-15e585368791)} did not return a View from onCreateView() or this was called before onCreateView(). at androidx.fragment.app.Fragment.requireView(Fragment.java:1780) at com.example.android.drawableanimations.demo.animated.AnimatedFragment$$special$$inlined$viewBindings$1.getValue(ViewBindingDelegates.kt:50) at com.example.android.drawableanimations.demo.animated.AnimatedFragment$$special$$inlined$viewBindings$1.getValue(ViewBindingDelegates.kt:39) at com.example.android.drawableanimations.demo.animated.AnimatedFragment.getBinding(Unknown Source:2) at com.example.android.drawableanimations.demo.animated.AnimatedFragment.access$getBinding$p(AnimatedFragment.kt:29) at com.example.android.drawableanimations.demo.animated.AnimatedFragment$onViewCreated$1.onAnimationEnd(AnimatedFragment.kt:45) at androidx.vectordrawable.graphics.drawable.Animatable2Compat$AnimationCallback$1.onAnimationEnd(Animatable2Compat.java:86) at android.graphics.drawable.AnimatedVectorDrawable$2.onAnimationEnd(AnimatedVectorDrawable.java:1021) at android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT.onAnimationEnd(AnimatedVectorDrawable.java:1771) at android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT.lambda$callOnFinished$0(AnimatedVectorDrawable.java:1778) at android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT$$ExternalSyntheticLambda0.run(Unknown Source:4) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7829) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)

shreelakshmijoshi commented 2 years ago

Hi @amazingvoice ! I am a new open source contributor and would like to solve this issue, could you help me understand the issue better by providing some extra references ? Is this is issue open to contribute ?

amazingvoice commented 2 years ago

Hi @shreelakshmijoshi! Just run the demo DrawableAnimations on any android phone or emulator, follow the steps I provided:

start the app DrawableAnimations -> click AnimatedVectorDrawableCompat -> click START -> back press before the animation is done,

then check the logcat to find the stacktrace, you can set some breakpoints to help you understand how it works.

shreelakshmijoshi commented 2 years ago

The animated fragment is set to null after the animation is completed. When we press the back button , the view gets destroyed but the animated fragment which is bound to the view is still running. We should also destroy the object associated with the view when back button is pressed. That's why OnBackPressedCallback function is added which implements the operation of the stop button

  override fun onAttach(context: Context){
        super.onAttach(context)
        val callback: OnBackPressedCallback =object:OnBackPressedCallback(
        true
        ){
            override fun handleOnBackPressed(){
                binding.stop.performClick()
                activity?.supportFragmentManager?.popBackStack()
            }
        }
        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            callback
        )
    }
shreelakshmijoshi commented 2 years ago

@amazingvoice please review my pull request link

amazingvoice commented 2 years ago

@shreelakshmijoshi Sorry I'm not the maintainer of this project. @ggfan Please help review this PR. Thanks!

shreelakshmijoshi commented 2 years ago

oh okay..

shreelakshmijoshi commented 2 years ago

@ggfan Can you or someone else help me review and merge the pull request ? Thank You!