App Launch time & Activity creation (Android Performance Patterns Season 6 Ep. 2)


[MUSIC PLAYING] COLT MCANLIS: As developers,
there’s nothing we like more than an amazing
activity that delights and entertains our users. But sadly, initializing all
the subsystems, resources, and layouts to provide that
experience takes a bit of time and memory, meaning
the users are going to have to wait for
their transcendent experience. My name is Colt McAnlis, and
one of the most important things that you can do for the
health and performance perception of your application
is optimizing the performance of loading your activities. Remember, when the
user launches your app, they’ll be shown a start window
while your app is loading and getting ready to display. Now, most of the time, if your
user is spending too much time staring at the
screen, the problem is most likely to be in the
creation of your main activity. I mean, a bunch of
stuff happens here. You’re setting the theme data,
inflating view hierarchy, loading bitmaps,
deserializing network fetches, writing user
preferences– you know, all the stuff you need
to display your activity. But if you’re not careful,
each of these little operations can fall into their own
performance pitfalls– things like multiple garbage
collection events or waiting to inflate views or
blocking on network requests– all things which can
delay the time it takes for the first pixel
to be shown to the user. So if you’d like to streamline
your activity loading to give users the best
experience possible, here’s what you need to do. First, figure out
if you actually have a problem, which you can
do with the method tracing tool. This will give you a
snapshot of your call stack and let you visually
see what functions are taking longer than others. If the onCreate function
of your activity is taking longer than
16 to 30 milliseconds, then it’s worth exploring
more to see what might be causing the delay. And this can be done by
inserting trace commands at various parts of your code. When you do this, the
timing between the begin section and end sections
will be logged and then displayed in your
Systrace captures so that you can see more
information about where your time is going. Now, if you’ve found that your
load time is taking too long, hopefully you’ve been
able to narrow it down using the
inline tracing code. If not, here’s a couple
of common problems and how to address them. Firstly, the most common problem
we see with slow activity loads has to be the time it takes
to inflate a view hierarchy. See, the larger your
hierarchy, the more time it’s taken to inflate
it, and the more bitmaps or resources it requires to
load also adds to its load time. As such, anything you can
do to flatten the hierarchy, reduce complexity, or stub out
portions that aren’t visible right now will help to
reduce the time spent doing this work at the
creation time of your activity. Secondly, try to
structure your code to rely on lazy loading
and async processing as much as possible. Commonly, the onCreate
method of your activity becomes a junk
drawer for all sorts of different
initialization routines. Some of those things do
need to happen right now. Others can be
delayed until later. This includes things like
doing initial minimal loading of your hierarchy, loading
all of your initial images in an async fashion, and
waiting to initialize heavy [INAUDIBLE] systems until
they are absolutely needed. Really, the lazier you
can make the creation and loading of your activity,
the better your performance will be. But that doesn’t
mean you need to be lazy about the rest of your
application performance, which is why you should check
out the rest of the Android Performance Patterns videos. And don’t forget to join
our Google+ community too. So keep calm, profile your
code, and always remember #perfmatters. [MUSIC PLAYING]

Author Since: Mar 11, 2019

  1. Hey guys,
    try my new arcade game Brick Pong, it looks easy but is actually hard af!
    Google Play: https://play.google.com/store/apps/details?id=brickteamdevelopment.brickpong
    What you think about it?

  2. thank you for this video , its like you are the only guy whos working at android team this summer, where are the rest of th team ? ! !

  3. I appreciate posting the links to the related lessons it's really annoying when you just mention the video without a link. thanks ! I'' checkout the method tracing video later

  4. Are you a Web Developer?
    Surprise and at the same time make your colleagues laugh with NEW Web Developer must-have T-Shirt
    https://teespring.com/web-developer-humor
    – Limited Offer

Related Post