Search code examples
androidandroid-animationlistenerandroid-architecture-navigation

Prevent views from being clicked until animation added to action via navigation component completes


Given 2 Fragments A and B, A moves to B (so A -> B), via navigation component action with enter animation has been added. How to prevent views in Fragment B being clickable while enter animation is running? I've found this question How to add listener to android Navigation Architecture Component action animation but unfortunately there're no answers.

What I found in the documentation is that I could get resource ID of that animation through NavOptions object hooked onto the NavAction, but not the Animation object itself.


Solution

  • You can start by having your views as disabled in xml android:enabled="false" then in your fragment's onViewCreated you can set a delay with the animation duration using coroutines:

    override fun onViewCreated(view: View, savedState: Bundle?) {
        super.onViewCreated(view, savedState)
    
        // Initialize views here. 
    
    
        lifecycleScope.launch {
            delay(resources.getInteger(R.integer.anim_duration).toLong())
            // Enable views here
            myView.isEnabled = true
        }
    }