android / animation-samples

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

handle back button key event #69

Closed shreelakshmijoshi closed 3 years ago

shreelakshmijoshi commented 3 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. As a result of the animation running even after back button is pressed, once this running animation completes, it invokes callbacks which try to access the destroyed view and its objects like the start and stop buttons at this place in the code

So ,since we are accessing a destroyed view in callbacks ( after pressing back button) , android framework throws an illegal state exception 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
        )
    }
google-cla[bot] commented 3 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

google-cla[bot] commented 3 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

shreelakshmijoshi commented 3 years ago

@googlebot I signed it!

yaraki commented 3 years ago

We should also destroy the object associated with the view when back button is pressed.

What object does this code destroy?

shreelakshmijoshi commented 3 years ago

@yaraki my bad ! this code doesn't destroy any object , it will perform click on the stop button.. and end the animation, when the AnimatedFragment View gets destroyed.. I have updated the earlier comment to reflect this.

shreelakshmijoshi commented 3 years ago

@yaraki , this is the error stack trace this is a clip 👇 to shows that would be no errors after pressing the back button

https://user-images.githubusercontent.com/74366348/130925401-15fa8e05-2d39-4333-a603-c5246cae55e5.mp4

yaraki commented 3 years ago

OK, I see what you mean. But this should be fixed by unregistering the callback when the fragment's view is destroyed. Could you do it that way? Otherwise I can do it.

shreelakshmijoshi commented 3 years ago

@yaraki , Yes that would work too, I did that change to un-register / clear all callbacks , during view destruction . Please review and approve the latest change in the newer PR here I will be closing this PR at this point because we have a newer pull request.

Thank You!