Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

How to use MaterialFadeThrough Transition as StackTransactionModifier? #82

Closed
wman1980 opened this issue Dec 22, 2020 · 6 comments
Closed

Comments

@wman1980
Copy link

Hi,

very nice library!

Currently I am asking myself how to implement MaterialFadeThrough-Transition during a stack-change?

I was able to implement a Cross-Fade Transition by using stackTransactionModifier of MultiStackNavigator like this
this.stackTransactionModifier = { setCustomAnimations( R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out ) }

But how to do this by using a MaterialFadeThrough Transition?

@tunjid
Copy link
Owner

tunjid commented Dec 22, 2020

Hi @wman1980, thanks for checking out the library. MainActivity has an example of this using an extension:

navigator.stackTransactionModifier = navigator.materialFadeThroughTransition()

The full implementation is:

fun MultiStackNavigator.materialFadeThroughTransition(): FragmentTransaction.(Int) -> Unit = fade@{ index ->
    val rootFragmentManager = current?.activity?.supportFragmentManager ?: return@fade

    rootFragmentManager.findFragmentByTag(activeIndex.toString())?.apply {
        enterTransition = null
        if (exitTransition !is MaterialFadeThrough) exitTransition = MaterialFadeThrough().setDuration(300)
    }
    rootFragmentManager.findFragmentByTag(index.toString())?.apply {
        exitTransition = null
        if (enterTransition !is MaterialFadeThrough) enterTransition = MaterialFadeThrough().setDuration(300)
    }
}

@wman1980
Copy link
Author

ahh, that is awesome!

I just forgot about the supportFragmentManager and the index.

Thanks a lot!

@wman1980
Copy link
Author

wman1980 commented Feb 25, 2021

Hi @tunjid,

I have a follow up question.

If I add some Transition.TransitionListener to that code above to listen to start and end events for each transition. Do you have an idea how I could pass these events to my active and target fragment?

Currently the fragment returned by findFragmentByTag is a StackFragment and it does not offer a lot to me because everything is internal ...

I first though I could just add this listener within my active/target fragment, but there the transition is null. So is there actually a way to get these transitions from my active/target fragment?

Thanks

@tunjid
Copy link
Owner

tunjid commented Feb 25, 2021

@wman1980 MultistackNavigator.current and MultistackNavigator.previous should give you you a reference to your Fragment in your Transtion.Listener. Do you have a snippet of what you're trying to achieve with the listener?

@wman1980
Copy link
Author

wman1980 commented Feb 25, 2021

Actually I have a WebView within android and the webview "flickers/blinks" if a transition (MaterialSharedAxis.X) is started. My plan to prevent that was to create a bitmap snapshot and put that snapshot in front of the webview on transition start and remove that snapshot on transition end. But unfortunately I just discovered that onDestroyView of my fragment is called before onTransitionStart e.g. during a returnTransition. Damn android!

@wman1980
Copy link
Author

ok, I worked around my issue by creating a snapshot onPause within my fragment. Sorry to spam with unrelated stuff and thanks for your response.

@tunjid tunjid closed this as completed Feb 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants