Android Activity Startup Optimization

1. Overview

Activity(http://developer.android.com/reference/android/app/Activity.html) is the main class for app in Android.It’s used to manage gui lifecycle.

The gui drawing elements, like View,ViewGroup. Must be put in Activity. So for app developers, Activity is the first class they have to use and understand. And without Activity, nothing can be shown.So for the App developers, create an activity is the first thing in their code.

Activity is used to manage gui lifecycles. To use gui elements like View, ViewGroup, we have to create an Activity first. So when we start a new app on device, we have to create an Activity before drawing in the View. So time cost in Activity creatation will delay the drawing in View for the first time. It means an obvious delay to the end user when they start an app or switch gui which is managed in new Activity if create an Activity cost a lot of time.

Some app creators meet the problem of Activity starting time costs. They cannot solve the problem in their apps. What they can do is trying to keep Activity in memory, so when users switch to the gui for the second time, they will not feel delay. But the delay for the first time is still there.

And the Activity starting time cost is larger on the low level device. So the end user will suffer more when they are using a cheap device. It will prevent Android from larger its market share in the low level devices. On high level devices, the time cost is obvious comparing to iOS devices, it causes a bad reputation on Android when compare to iOS.

2. Startup Sequence Analysis

There is a detail startup sequence here(in chinese):

http://blog.csdn.net/zhengg21/article/details/8638669

There are 2 circumstances. One is start activity in new process. The other is start activity in the exsiting process. Time cost is diffrent for the 2 cirumstances.

There are 2 main process. One is in the trigger process, the other is in the new created process. And the time cost is as below:

Android version: 4.4.2 Hardware: Hisilicon 3716cv200

trigger app

new app

(tme in millisecond)

process start to onCreate start

in onCreate

onCreate end to onDraw start

total

start activity in new process

136

122

86

243

587

start activity in existing process

33

2

98

123

256

As the data above, to start an activity in an existing process is much faster than to start an activity in new process.

Time cost for start activity in new process is:

2.1. Phase 1

more time in circumstance 1 is consumed in

ActivityStack.startPausingLocked
Zygote.fork

This is something has to be done to start a new process.

2.2. Phase 2

more time in circumstances 1 is consumed in

ActivityManagerService.attachApplication
Activity.performCreate
ActivityThread.handleResumeActivity
Choreographer.FrameDisplayEventReceiver

This is somethong has to be done to link the new activity to the activity manager service.

2.3. Phase 3

time cost is equal to the 2 circumstances.

Activity onCreate cost time in this phase. It’s for activity resource preparation.

2.4. Phase 4

more time in circumstances 1 is consumed in

Opengl context init

3. Conclusion

As the analysis above, time cost for start activity is in a lot of pieces. No where cost most of the time. And for each phase, time cost is reasonable. So fail to find a way to optimize it.

Platform/Android/ActivityStartup (last modified 2014-06-30 00:23:42)