How can I switch to another fragment without any animation?
I have two fragments and show/hide them using this:
supportFragmentManager
.beginTransaction()
.hide(currentFragment)
.show(selectedFragment)
.setTransition(FragmentTransaction.TRANSIT_NONE)
.commit()
I expect them to switch without any animation, but there is a fade animation.
If I set the transition to TRANSIT_FRAGMENT_FADE, The fade animation will be shorter:
I'm using androidx version 1.6.0-alpha02 and kotlin version 1.4.32
Thank you.
FragmentTransactions sets the animation for a fragment entering or exiting the transaction.
show(): displays a previously hidden fragment. This is only relevant for fragments that have been added to the container. Show will use the FragmentTransaction that was used when the Fragment originally entered the container.
hide(): Hides the existing fragment. This is only relevant for fragments whose views have been added to a container, as this will cause the view to be hidden. Hide will use the FragmentTransaction that was set when the fragment originally entered the trasaction.
In your case when you call FragmentTransaction, it isn't setting the animation because you don't have a fragment entering or exiting the container, you are using hide and show to display your fragment so the FragmentTransaction is using the animation for when the fragments had originally entered the container.
If you wish to control the animation I would suggest using replace instead of show and hide`.
I found the problem.
It was because of Shared Element Transition that was setting in onCreate.
sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
Removing this line fixed the problem.
However I wonder in case of having a shared element transition, how can I navigate to another fragment without fading animation...
Related
I have nested fragment, fragment inside fragment. Using NavigationBottomView. Everything goes right when I backpress from parent to child fragment.
But now, if I am inside child fragment and suddenly my phone screen goes off, and if I Open the screen again and backpress from where I left, it's not going I think it is forgot or clear my backstack. Don't know why. I have made a demo on github link below
MessageListningFragment messageListningFragment = new MessageListningFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack( null );
transaction.replace( R.id.dynamic_container, messageListningFragment ).commitAllowingStateLoss();
github demo link is here
You should either use a ViewPager/ViewPager2 with NavHost fragments or implement BottomNavigationView with extension functions Google provided, which basically tags fragments and add them with NavHostFragment.create().
NavHostFragment takes care of childFragmentManager and back stack management. You can check out examples here if you wish.
I CANT use ActionToolbar because this is a KIOSK app with a custom header fragment that needs to be displayed at all time. Hence my situation, I have a basic ImageButton on the said header fragment, and I want to implement the functionality of a basic ActionToolbar.
I add my fragment to the main container like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, UserListFragment.newInstance())
.addToBackStack("foo")
.commit();
The question is, how do I restore the state of my main_container before the current fragment was replaced with UserListFragment.
Like : fragment A is showing, replacing fragment A with B, pressing some button in my header fragment to pop the fragment B so again fragment A is showing.
You should consider switching to Android Jetpack Navigation Component it was created for your type of situation.
Check: https://stackoverflow.com/a/62252603/8714139
Cheers!
Im using Navigation Architecture Component and i got my shared element transition between two fragments working fine. First fragment contains recyclerview and second one is a "details" of recyclerview item. The problem is that i need to keep first fragment visible while transition in progress.
How do i make transition like that ? Is there any option to achieve this with Navigation Architecture Component ? If no, what i need to use ?
The way exit transitions work is by adding the target views to a ViewOverlay that is added to the entering fragment. This means that, once the fragment is replaced, only the entering fragment exists and all the transitions applied to views that are exiting are actually happening on views that have been added to the entering fragment's ViewOverlay.
The reason why your exiting fragment doesn't stay visible is because you don't have an exit transition which means that no views are added to the ViewOverlay of the entering fragment.
Each view that is targeted in the exit transition is added to the ViewOverlay which sits on top of the entering fragments view hierarchy. So having the exiting fragment just sit there whilst the transition happens is not possible because once you target any views of the exiting fragment it will cause them to be on top of the view hierarchy, thus hiding all the other views (including the shared element).
Your best bet is probably to use a Fade transition as the exit transition and this way you can create a cross fade between the fragments
I dont know is it really possible with Navigation.
But its really possible with basic FragmentTransaction
fragmentManager!!
.beginTransaction()
.setReorderingAllowed(true) // setAllowOptimization before 26.1.0
.addSharedElement(view.imageIV, "fragment_news_iv")
.addSharedElement(view.title, "fragment_news_title_tv")
.addSharedElement(view.date, "fragment_news_date_tv")
.addSharedElement(view.container, "fragment_news_scrollView")
.addSharedElement(view.description, "fragment_news_description_tv")
.addToBackStack(null)
.setCustomAnimations(
android.R.anim.fade_in,
android.R.anim.fade_out,
android.R.anim.fade_in,
android.R.anim.fade_out
)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.add(
R.id.main_host_fragment,
NewsDetailsFragment(),
NewsDetailsFragment::class.java.simpleName
).addToBackStack(null)
.hide(this#MainFragment)
.commit()
Here is part of code which is doing transaction.
U can set custom animations like fade_in, fade_out with long duration(1000ms)
.setCustomAnimations(
android.R.anim.fade_in,
android.R.anim.fade_out,
android.R.anim.fade_in,
android.R.anim.fade_out
)
And set sharedEnterTransition in detailsFragment with duration 200
val trans = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
trans.apply {
duration = 200
enterTransition = trans
}
sharedElementEnterTransition = trans
So its obvious that first fragment will still be visible while transaction will be in progress. This is kinda workaround solution, but works)
I have 2 fragments the first contains a button which on clicked opens a fragment with a ListView in it. I have a shared element transition for the button to transition into the new fragment (root layout) but I would also like to have this transition in reverse (the list fragment contracts into the button again).
However currently I detect the list item click and send an event to the Activity which pops the listview fragment off the backstack (popBackStackImmediate()) hence does not show the transition.
Is there a good way to allow back navigation while preserving reverse transition to work as well?
did you try this:
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
fm.executePendingTransactions();
PS: use getSupportFragmentManager() if you're using the support library else use getFragmentManager().
I'm trying to add animation to my android app, and knew a lot about transition animation between activities, and about fragments. So I have MainActivity with some view (image), and SecondActivity which is empty itself, but contains fragments, which then needs to be used to display some detail information about this image. I've read about postponeentertransition() and it worked. But the problem is, that this fragment in second activity have links(or transitions) to the same kind of fragment.
Let's say in main activity I have set of images of cars, then I click one, second activity is starting and waiting for fragment loading, and then shows animation. And then, in this fragment I have "similar cars" recycler, and choose one of them, and wants, to show its details the same way, but with adding this fragment to the backstack (not replacing). So the problem is that transition name of the view in main activity and big image of fragment are the same (which is obvious), and transition name in fragment's recycler and big image of the next fragment have to be the same too(otherwise app doesn't know how to make the animation). On the other side, fragment (which was added) has to be the same class, but its different instance.
I just want to set up animation without crashing the existing architecture.:) If you have any solutions or advices how to make animation between different instances of one fragment, please help.)
Use setCustomAnimations on the Fragment transaction (see below). It doesn't matter if the instances are from the same class.
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
// To animate the fragment
// NOTE: Must be before replace/add otherwise it doesn't work
transaction
.setCustomAnimations(enter, exit, popEnter, popExit)
.replace(R.id.frame_container, fragment, null)
.commit();