I am working with some fragments but the transition animations when switching from one to another is very laggy and sometimes it even skips the entire animation (which I've read that sometimes is an outcome of poor fragment performance).
The very intriguing thing is that all my data fetching and computation in that fragment is done completely async using coroutines set on computation threads. And the array lists containing the data are not even long or having many parameters. The only thing that I do on the UI thead is to set the final list on the RecyclerView Adapter and notifyDataSetChanged() -> that's all! So my conclusion is that the ViewHolder inflation itself is what causes the lag. The layout XMLs for the ViewHolders are as well very lite (a drawable res and three strings).
In terms of fragment transactions, I replace the current fragment with the new one in my FrameLayout and commit using a custom set of XML object animators.
I've seen some answers saying that I should delay setting the items on the Adapter for about 2 seconds, but hell, it would look really really bad for an end user to navigate in that fragment and wait 2 seconds on a white screen before the data suddenly shows up. I want a smooth 60fps transition to the new fragment, with the data already displayed.
Is this thing even possible on Android? I am not an apple fan, but as far as animations flow and smoothness, iOS devices wreck havoc on the mess that is Android at this subject.
I need any kinds of advices or even examples to show me how to fix this issue.
Thank you!
The Fragment API provides two ways to use motion effects and transformations to visually connect fragments during navigation. One of these is the Animation Framework, which uses both Animation and Animator. The other is the Transition Framework, which includes shared element transitions.
At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.
Fragments support AndroidX transitions . While fragments also support framework transitions, we strongly recommend using AndroidX transitions, as they are supported in API levels 14 and higher and contain bug fixes that are not present in older versions of framework transitions.
Once you've defined your animations, use them by calling FragmentTransaction.setCustomAnimations () , passing in your animation resources by their resource ID, as shown in the following example: Note: FragmentTransaction.setCustomAnimations () applies the custom animations to all future fragment operations in the FragmentTransaction .
Try add a startOffset (like 300ms) when add/replace the fragment, fragment will pre-load from background, after 300ms, app will run the animation show your fragment smoothly.
300ms is just a random number, it's really depends on how heavy UI you need to init.
Hope this help.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="400"
android:interpolator="@android:interpolator/accelerate_decelerate"
/>
<translate
android:startOffset="300"
android:fromYDelta="100%"
android:toYDelta="0%"
android:duration="400"
android:interpolator="@android:interpolator/accelerate_decelerate"
/>
</set>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With