Android Cold boot optimization 1. What is cold start
Cold start is the case when the application is first started or the application is killed (not in memory) and then started again.
You may have noticed that the startup time will be slightly longer in this case. Because it will initialize the resource again (application, etc.).
When the app starts, it usually initializes in application or so-called splashactivity.
Suppose there is too much work in the application. So when the cold starts. There will be a 白屏
situation. Because SplashActivity
it has not been initialized at this time. SplashActivity
the picture on it hasn't been shown yet.
We certainly do not want 白屏
the situation to arise.
The solution should be considered in two ways, and 1 is the Application
initialization logic of the optimization. This asynchronous asynchronous, for example, delays the delay. 2 is to be 白屏
replaced SplashActivity
in the display of the picture, so that users have an application has been started the illusion, this article is about this optimization scheme.
2. How to Optimize
We need to SplashActivity
set one Theme
, for example the following:
Styles.xml
name="AppTheme.Launcher"> <itemname="android:windowBackground">@drawable/launch_screen</item> <itemname="android:windowFullscreen">true</item> </style>
This Theme
inherits the basic theme of the app AppTheme
, the same time the property is replicated windowBackground
, its value SplashActivity
will show the picture (logo and so on ...).
Launch_screen.xml
<?xml version= "1.0" encoding= "Utf-8"?><layer-list xmlns:android="Http://schemas.android.com/apk/res/android" Android:opacity="opaque"> <item android:drawable="@android: Color/white"/> <!--Your product LOGO-144DP color version of Your app icon -- <item> <bitmapandroid:src= "@drawable/splash_defalut"android:gravity=" Fill "/> </Item></layer-list>
Then set this topic to the SplashActivity
top:
<activityandroid:name=". Ui.activity.SplashActivity"android:configchanges ="Fontscale"android:screenorientation= "portrait"android:theme="@ Style/apptheme.launcher " > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter></activity>
This is not finished, because we SplashActivity
set a background map for more, it is bound to cause the transition to draw, so we before its initialization. Set the theme back to the original:
@Override protectedvoidonCreate(Bundle savedInstanceState) { setTheme(R.style.AppTheme); super.onCreate(savedInstanceState); setContentView(R.layout.splash_activity);
Above.
Now you will find that even if the boot time is longer, it will not appear 白屏
.
References:
- Https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd
Android Cold start Optimizer