Triggering a fragment's transition animation without popBackStack() - android

I've chosen to hide/show my fragments during navigation for smoother animations.
To display a fragment, I add an animation :
transaction.setCustomAnimations(R.anim.from_right,
R.anim.to_left, R.anim.from_left, R.anim.to_right);
Then I simply hide previous one, show next one.
The problem is if I want to "popback" the fragment, I don't use the normal addToBackStack(null);
Instead I just hide the previous one and show the next one again. But this way the animation is not played. I don't want to save which fragment should use which animation every time.
How do I use the animations I initially set up in setCustomAnimations when I leave the fragment, in the same way a popBackStack would do it?

Related

Android Fragment Manager, change custom animations after commit

I have a fragment manager, and I am using custom animations with it. But the idea is that the user can swipe a fragment away, and I need a way to change the exit animation based on the user swipe. Is there a way I can setCustomAnimations on an already committed transaction?

How to reset MotionLayout to first ConstraintSet

I use a ViewPager have 3 pages. How to reset MotionLayout and re-run animation when a page is re-selected?
Currently, I call method transitionToStart() when a page replaced by another. But if user swipe back too quickly, animation transitionToStart() won't finish.
Is there a better method?
You can do the following:
motionLayout.setTransition(R.id.transition);
motionLayout.transitionToEnd();
if (instant)
motionLayout.setProgress(1);
You can manually set the progress by calling .setProgress(0) or .setProgress(1)
Based on .setTransition(R.id.transition) you can define start and end constraint (in case you have more than two constraintSets).

Prevent last Fragment from being removed from backstack

I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks
The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.
I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.

Animation between fragment transactions

I am try to do two animation while navigation from one fragment to other fragment.
When I am in onefragment,On the tab click of activity,current fragment should slide left and on the launch of new fragment it should be sliding from right.
I tried intially using FragmentTransaction's setCustomAnimations(),but it was not successful.
So I am doing animations on status of fragment.So the SlideLeft transititionm,I am doing in Stop() of the fragment,While SlideIn transistion has been done on OnAcitvityCreated() of the fragment.
Please let me know Is this correct way or any other appropriate way to handle this

Android Honeycomb: layout problem - hide/show FrameLayouts

in my Activity, I have a layout containing 3 FrameLayouts, one at the top, one at the left and one at the "center".
Now, I sometimes only want to display one or two of them. Atm I am doing it this way:
FrameLayout frame = (FrameLayout) findViewById(R.id.framelayout_menu_left);
frame.setVisibility(...);
frame = (FrameLayout) findViewById(R.id.framelayout_content);
frame.setVisibility(...);
frame = (FrameLayout) findViewById(R.id.framelayout_menu_top);
frame.setVisibility(...);
However this can get really ugly results, e.g. when I switch the "content" Fragment and hide the top and/or left FrameLayout. It all starts flickering as the "content" Fragment jumps to the top and/or left and only afterwards is replaced.
Also, I can obviously not navigate back to another setup, so is there any other way to do this?
Kind regards,
jellyfish
Edit:
Maybe a little drawing makes my question clearer...
A shows a Layout of 3 FrameLayouts containing 3 different Fragments. Each color represents one distinct Fragment.
Now what I want to do is to switch from A to D.
I am doing this by replacing the blue Fragment with the yellow Fragment via a FragmentTransaction.
However, this still keeps the other Frames visible, so I hide them via the code above.
Now, Frame.setVisibility() is called way before commit(), so in B and C the blue Fragment "jumps" to the left and the top and only afterwards (in D) is replaced with the yellow Fragment. This produces a nasty flickering.
As a workaround, I now hide all three FrameLayouts before the transaction and re-show the ones I need once the transaction has finished. But there still is the problem that I can't go back via the back button as this isn't a real transaction.
I would have two suggestions. Firstly, if you both add a fragment transition effect and do the visibility changes after the transaction, that would probably substantially reduce much of your flicker effect
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Secondly, I've simply given up on having the system manage the fragment stack for me -- it seems that this only works well with simple transactions. Override onBackPressed and do your own logic there.
--randy

Categories

Resources