Facing issue with setCustomAnimations() while replacing fragments - android

I have 2 fragments A and B. I am replacing the fragment B with A, and setting setCustomAnimations(right_to_left, fadein, fadeout, left_to_right). Fragment B is behaving correctly like it is coming from right to left and on pressing Back button and do pop back stack it is leaving from left to right. But no animation happens to the fragment A its doesn't fade in and fade out.
Note: I have tried it with creating the sample application and it works perfectly fine but when I implement the same thing into my project it behaves as above. What mistake am I making while implementing the setCustomAnimation() in the my project?
I have already tried:
have a latest support library
hardwareAccelarated="true" in the manifest file
add setCustomAnimation() before replace() method
compile it with above 3.0
I doubt there can be an issue with the framework my app is built on but not sure. Can anyone please let me know what else I can check to make it work?

Related

After navigating with popUpTo, the next fragment shows back button in NavBar

So I have a standard scenario, described in the Android dev docs.
Both fragments are so far pretty much stock from the New | Activity | Basic Activity wizard. I've only slightly changed the first fragment so far to start working on the login credentials.
Basically, after I complete the login, I want to clear my call stack and just move to the main app screen.
This works to some extent for me, by adding:
app:popUpTo="#id/FirstFragment"
app:popUpToInclusive="true"
and then navigating
NavHostFragment
.findNavController(this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
It works because when pressing the back button / performing the gesture, the app will simply close.
What DOESN'T work, is that now the title bar for the second fragment is showing the back arrow. The back arrow doesn't even work as there's no stack to pop, but still it's there.
I've investigated answers to this question.
I cannot seem to get the FirstFragmentDirections to build, so I can't try that solution. Tried (unsuccessfully) to modify gradle file and searching for a file that would be built, but not accessible.
I am now left with an arrow that has no place where it's rendered and would appreciate any pointers on how to get rid of it.
Edit1: I have now discovered how to get the FragmentDirections class to be generated. It's described here - all the SO suggestions on the matter lacked the repository inclusion and only mentioned the plugin inclusion.
Edit2: The arrow depends on fragment properties. One of such properties is navigation's app:startDestination. It will start the activity on this fragment, but also display the back arrow on all other fragments.
My task has shifted from total non-understanding to seeking how I can specify / override this per-fragment.

Android fragments popBackStack

I have the following issue and I'm no quite sure whether it is a bug or misunderstanding by me.
I have a single activity, Fragment A (attached to activity's onCreate Method) and Fragment B (not attached to activity during its creation). On button click, Fragment A is replaced with Fragment B with transition. So far so good.
On Fragment B there is button which invokes fragmentManager.popBackStackImmediate(). If I press this button and turn the screen off at the same time (during fragment transition) then, when the screen is back on, the two fragments are visible (fragment B should have been destroyed, but it is not).
This problem starts with support lib 27.1.0+. Prior to that, the fragments were replaced correctly during Back stack navigation. Have in mind that I'm using fragments from support library.
I read behavior changes but I could not manage to link some of it to my issue.
Does anybody knows some work around or has more information about it?
Thanks in advance!
UPDATE:
Problem exists only on devices up to Android 6. On Android 7+ everything is alright.
fragmentManager.beginTrasaction().replace().addToBackStack() - do it for FragmentA in onCreate and do it on click for FragmentB. And try popBackStack(). I think you forgot addToBackStack FragmentA.

Android Studio multiple fragments

I'm designing a music player app and this is my second time working with android studio and fragments in general. I've watched a lot of tutorial videos on this but I'm having trouble getting multiple fragments to show. Maybe using fragments isn't the way of going about this. But basically what I have right now is a fixed menu at the top and 5 fragments just below it (on top of each other).
What I was hoping to do was by clicking each button one fragment would load so you would have one activity but 5 fragments that load when buttons are pressed. But the issue i'm having right now is instead of stopping one fragment and loading another one, they're being loaded on top of each other. Is there a way to go about doing this?
Below is an image of the design I had in mind. The blue box is where the five fragments are and right now they're overlapping. Any help would be appreciated :)
EDIT: Turns out the answer was I needed to define one layout for all of the fragments. Thanks everyone.
Please post code. But here's what I can make out so far from your problem,
I assume you are just using FragmentManager.beginTransaction.add() again and again.
If it is the case please change your code to
FragmentManager.beginTransaction().replace(
containerViewID,
new FragmentYouWantToReplace
);
What this does is removes previous fragment and adds this new fragment.
So you can have your default fragment using add() during activity load, but for each button click you use replace().
When you call a new fragment you should replace always the same layout, the one you want to show the information, like this:
frag_results frag_results = new frag_results();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.framelayout_secundary, frag_results, frag_results.getTag()).commit();

Android: backbutton animation when orientation is changed

I have a rather weird problem. Everything is working as it should except for this little thing. I'll show the code.
// ...lots of other code
ft.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left, R.animator.slide_in_right, R.animator.slide_out_left);
ft.addToBackStack(null);
// ... some more code
It's pretty simple, when I click a button that replaces a fragment with another fragment an animation between the two is played out. This works dandy! And as you see, the same animation is presented when I press the back button.
It all works! Until... I change orientation. Let's say I'm at the third fragment, change orientation and press the back button, the animation does not play out. The previous fragment is displayed instantly. When I click a menu button again to the third fragment the animations all of a sudden start to work again.
I'm guessing this has something to do with the Bundle, as when you change orientation the Activity is stopped, then started and the savedInstance being "reinserted". However the savedInstance animation was made for portrait and not landscape. Or it's simply just not saving the setCustomAnimations().
Is this a bug or is this how it is supposed to be?
As far as I know, this is a reported bug both in the native and support libraries.
Look at this question, or here for some advices. I tried the last advice in the second link and it works fine now.

Animating fragments and the back stack

I am having trouble using or understanding how popping
FragmentTransactions off of the back stack handles the custom
animations. Specifically, I expect it to call the "out" animation, but
it doesn't seem to.
I have a simple method to handle a fragment transaction
(FragmentTransaction) where I add a fragment and apply a custom
transition so that it will fade-in/fade-out. I am also adding this to
the back stack so that the user can undo that transaction with the
back button, essentially navigating to the state before the fragment
was added.
protected void changeFragment() {
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.add(R.id.fragment_container, new TestFragment());
ft.addToBackStack(null);
ft.commit();
}
Everything works great moving forward, but when the user clicks the
back button, the transition animations do not reverse. What I expected
was that when the fragment got removed, it would use the fade out
animation. Instead it seems to pop out (without animation) and then
the container seems to fade in. I'm not sure that this is exactly what
is happening, but the fragment is definitely not fading out.
My application uses the compatibility library to add fragment support,
but I assume this to be applicable to Honeycomb (android-11) as well.
Does anyone know if I am just doing something wrong here or if I am
just expecting too much? Ideally, I would like to animate the
fragments similarly to how Gmail (on the Xoom) does in regards to
moving forward by clicking a message and then back by using the back
button. Preferably not having to override the back button
functionality and keep up with my own fragment state since I could
have several "transactions" that I would want to back out of and I am
not a fan of re-inventing wheels.
Also asked on the Android Developers Group: http://groups.google.com/group/android-developers/browse_thread/thread/1136a3a70fa0b6e9
I use this:
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);
and the transitions work in reverse when the back button is presses.
The bug was fixed in the 3.2 release with the addition of the following new api:
http://developer.android.com/reference/android/app/FragmentTransaction.html#setCustomAnimations(int, int, int, int)
It's to be noted that it has not yet been back-ported to the compatibility library (as mentioned in the bug report).
It's a bug, look at bug report 15623. One of the Android project members commented that the fix was too late for release 3.1 but it should make it into the next release.
The same member goes on to say that...
The problem is that the same
animations are run on a pop operation
as were run to put the fragments in
their current places. For example, in
the sliding example above, on a
forward operation (pushing the old
fragment onto the stack and moving the
new fragment into view), we slide the
old fragment out from the center to
the left and slide the new fragment in
from the right to the center. When the
stack is popped, these same animations
are run: the most recent fragment is
animated 'out' by sliding it in from
the right to the center (after which
it disappears, since it's being
removed). The old fragment is popped
off the stack and animated from teh
center to the left ... right off the
screen.

Categories

Resources