Zhuinden / simple-stack

[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
Apache License 2.0
1.36k stars 76 forks source link

Ability to disable fragment transition animations #234

Closed topnax closed 4 years ago

topnax commented 4 years ago

It would be amazing if the default fragment state changer allowed us to disable fragment transaction animations.

Zhuinden commented 4 years ago

Hey hey!

While it's not the theoretically best design choice, it's a "less intrusive" solution in Java than it would have been in Kotlin - so you can extends DefaultFragmentStateChanger and override the following 3 methods:

    protected void onForwardNavigation(@Nonnull FragmentTransaction fragmentTransaction, @Nonnull StateChange stateChange) {
        fragmentTransaction.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    protected void onBackwardNavigation(@Nonnull FragmentTransaction fragmentTransaction, @Nonnull StateChange stateChange) {
        fragmentTransaction.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right, R.anim.slide_in_from_left, R.anim.slide_out_to_right);
    }

    protected void onReplaceNavigation(@Nonnull FragmentTransaction fragmentTransaction, @Nonnull StateChange stateChange) {
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    }

If it's overridden as empty, or maybe things like val topNewKey = stateChange.topNewKey<MyFragmentKey>(); if(topNewKey.isAnimationEnabled()) { super. then you can control whether animation happens or not.

Ultimate last resort can always be pasting the handleStateChange and customize it as needed, but that shouldn't be needed as just overriding the methods should be sufficient, and is already possible.

Zhuinden commented 4 years ago

Any luck?

Zhuinden commented 4 years ago

Technically this is already possible so I will close the issue.

uberchilly commented 1 year ago

What would be the way to disable animation for just one transition? Is that possible?

Zhuinden commented 1 year ago

@uberchilly heyo, by default, fragment animations can be customized by extending from DefaultFragmentStateChanger, and override either onReplaceNavigation, onForwardNavigation, or onBackwardNavigation.

If you don't call super.___ there it will not execute the default transition.

You can customize when you want to or don't want to keep it based on the stateChange parameter, which contains the new keys and the previous keys.