3

I have an issue with activities loading and then beginning any sort of animation. Usually by the time the activity is loaded up the animation is already half way complete or completely choppy.

And this is for mostly all cases: progress spinner animation, recyclerview list animations and so on so forth.

Is there anyway I can smooth out the animations when an activity is loaded? Maybe delay everything until the UI is ready to handle the animations and list load?

Here is my scenario.

  1. User clicks button to open activity, activity loads fragment
  2. Fragment onActivityCreated starts progress bar spinner and begins retrieving saved list items from shared preferences, or async network call if not cached (list is never more than 10 items, very small objects of ~8 strings).
  3. RecyclerView is loaded with adapter and adapter animates items by sliding/fading them in.

Both animations, the progress bar and slide/fade in are already half way complete by the time it is visible, or become very choppy. Is there any way to make this a bit smoother?

Here is the list as requested: enter image description here

Thanks.

Edit: Ill add, that on this activity, the fade in animation is non existent, and the slide in is about halfway complete when it loads.

SikhWarrior
  • 1,037
  • 5
  • 17
  • 34
  • Hello! can you please give a screenshot of your works? thanks – Kyle Emmanuel Aug 18 '15 at 04:34
  • @SikhWarrior: try to start animations after getting callback on `onViewCreated` method of fragment. – Mehul Joisar Aug 18 '15 at 04:49
  • @MehulJoisar Sorry, made a mistake in the question, everything is loaded in onActivityCreated. Would moving it to onViewCreated make a difference? – SikhWarrior Aug 18 '15 at 04:56
  • @SikhWarrior: ok. then it won't make any difference so I guess you need to move your animations in `onStart` method as it will be the first method which will get callback when fragment will be **visible** to users. [reference](http://developer.android.com/reference/android/app/Fragment.html#onStart()) – Mehul Joisar Aug 18 '15 at 05:02
  • @MehulJoisar moving it to onStart didn't make any noticeable difference. Although I did notice that subsequent fragments on the same activity dont have the same problem and work smooth. In this case I can remove the activity container and have it transition the fragment from where the button was pressed, but there are other parts of the app that need a new activity and suffer loading. – SikhWarrior Aug 18 '15 at 05:17
  • @SikhWarrior: I guess then it is matter of animation logic. Can you give a try directly by using other fragment instead of current one? If other fragment works smoothly with animations then it can be concluded that animation logic is wrong in your current fragment. – Mehul Joisar Aug 18 '15 at 05:22
  • @SikhWarrior:And for testing, try to forcefully do the async call instead of local cache. So you will be able to check whether animation is working fine or not.Because when you get data from local cache, it will get quickly and animations won't last long. – Mehul Joisar Aug 18 '15 at 05:24
  • @MehulJoisar yes that would make sense for the progress bar, but the recycler view animations wouldn't be affected by the async or cache speed since that happens after everything is loaded. – SikhWarrior Aug 18 '15 at 05:26
  • @SikhWarrior: in other fragments, do you have such recycler view animations? if yes, then try to replicate that fragment here just to check whether they are working fine or not. – Mehul Joisar Aug 18 '15 at 05:29
  • @MehulJoisar yes another fragment on the same activity has the exact same animations (same list item layout, same base adapter with animation) but works smoothly. Its clearly tied to when the activity first starts up, as any subsequent fragments animate perfectly. – SikhWarrior Aug 18 '15 at 05:30
  • @SikhWarrior: ohk. add some codes of activity and fragments so that I can checkout and trace the problem. – Mehul Joisar Aug 18 '15 at 05:33
  • @SikhWarrior were you able to resolve this? I'm facing the exact same problem and would appreciate your thoughts. – Vicky Chijwani Nov 29 '15 at 14:52
  • @VickyChijwani unfortunately no, I haven’t really found a work around. – SikhWarrior Nov 30 '15 at 22:56
  • @SikhWarrior I did find something of a workaround. I found that if I trigger my animations to start ~100 ms after onPreDraw it works fairly well. For more on how to use onPreDraw, Ctrl+F it on http://frogermcs.github.io/Instagram-with-Material-Design-concept-part-2-Comments-transition/. – Vicky Chijwani Dec 01 '15 at 06:21
  • @SikhWarrior one more thing, I'm pretty sure the answer we're looking for is buried somewhere in Nick Butcher's Plaid app, because it has super-smooth animations: https://github.com/nickbutcher/plaid/ – Vicky Chijwani Dec 01 '15 at 06:23
  • @VickyChijwani ill take a look at it when I get the time, thanks! – SikhWarrior Dec 01 '15 at 20:53

1 Answers1

3

As of API level 21, you can implement the Activity#onEnterAnimationComplete() callback and use that to start your animations. Unfortunately, it seems like there's no AppCompat equivalent at this point.

When I faced this issue, I decided to implement the onEnterAnimationComplete() callback on sufficiently high API levels, while on lower levels I simply fall back to starting animations in onCreate().

DriesOeyen
  • 483
  • 6
  • 13
  • I have been using that callback too and it seems to be working just fine on devices that run API 22 and above. On API 21 it seems that sometimes it's not getting called and sometimes it's called. Also, i found this SO question but no answers yet. http://stackoverflow.com/questions/39488410/onenteranimationcomplete-not-called-api-21 – KikiTheMonk Apr 05 '17 at 12:20