【转载】Android App应用启动分析与优化

前言:

昨晚新版本终于发布了,可是仍是记得有测试反馈app启动好长时间也没进入app主页,因此今天准备加个班总结一下App启动那些事!android

app的启动方式:

1.)冷启动

 当启动应用时,后台没有该应用的进程,这时系统会从新建立一个新的进程分配给该应用,这个启动方式就是冷启动。冷启动由于系统会从新建立一个新的进程分配给它,因此会先建立和初始化Application类,再建立和初始化MainActivity类(包括一系列的测量、布局、绘制),最后显示在界面上。swift

2.)热启动

当启动应用时,后台已有该应用的进程(例:按back键、home键,应用虽然会退出,可是该应用的进程是依然会保留在后台,可进入任务列表查看),因此在已有进程的状况下,这种启动会从已有的进程中来启动应用,这个方式叫热启动。热启动由于会从已有的进程中来启动,因此热启动就不会走Application这步了,而是直接走MainActivity(包括一系列的测量、布局、绘制),因此热启动的过程只须要建立和初始化一个MainActivity就好了,而没必要建立和初始化Application,由于一个应用重新进程的建立到进程的销毁,Application只会初始化一次。app

app的启动流程:

经过上面的两种启动方式能够看出app启动流程为:异步

Application的构造器方法——>attachBaseContext()——>onCreate()——>Activity的构造方法——>onCreate()——>配置主题中背景等属性——>onStart()——>onResume()——>测量布局绘制显示在界面上布局

app的启动优化:

基于上面的启动流程咱们尽可能作到以下几点测试

  1. Application的建立过程当中尽可能少的进行耗时操做
  2. 若是用到SharePreference,尽可能在异步线程中操做
  3. 减小布局的层次,而且生命周期回调的方法中尽可能减小耗时的操做

app启动碰见黑屏或者白屏问题

1.)产生缘由

其实显示黑屏或者白屏实属正常,这是由于还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。优化

示例:spa

2.)解决办法

经过设置设置Style线程

(1)设置背景图Themecode

经过设置一张背景图。 当程序启动时,首先显示这张背景图,避免出现黑屏

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:screenOrientation">portrait</item> <item name="android:windowBackground">>@mipmap/splash</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> </style>

(2)设置透明Theme

经过把样式设置为透明,程序启动后不会黑屏而是整个透明了,等到界面初始化完才一次性显示出来

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:screenOrientation">portrait</item> </style>

二者对比:

  • Theme1 程序启动快,界面先显示背景图,而后再刷新其余界面控件。给人刷新不一样步感受。
  • Theme2 给人程序启动慢感受,界面一次性刷出来,刷新同步。

(3)修改AndroidManifest.xml

<application android:name=".App" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true"> <activity android:name=".MainActivity" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> //...... </application>

解决后示例:

3.)常见的Theme主题
android:theme="@android:style/Theme.Dialog" //Activity显示为对话框模式 android:theme="@android:style/Theme.NoTitleBar" //不显示应用程序标题栏 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //不显示应用程序标题栏,并全屏 android:theme="Theme.Light " //背景为白色 android:theme="Theme.Light.NoTitleBar" //白色背景并没有标题栏 android:theme="Theme.Light.NoTitleBar.Fullscreen" //白色背景,无标题栏,全屏 android:theme="Theme.Black" //背景黑色 android:theme="Theme.Black.NoTitleBar" //黑色背景并没有标题栏 android:theme="Theme.Black.NoTitleBar.Fullscreen" //黑色背景,无标题栏,全屏 android:theme="Theme.Wallpaper" //用系统桌面为应用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" //用系统桌面为应用程序背景,且无标题栏 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系统桌面为应用程序背景,无标题栏,全屏 android:theme="Theme.Translucent" //透明背景 android:theme="Theme.Translucent.NoTitleBar" //透明背景并没有标题 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景并没有标题,全屏 android:theme="Theme.Panel " //面板风格显示 android:theme="Theme.Light.Panel" //平板风格显示

干咱们这行,啥时候懈怠,就意味着长进的中止,长进的中止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

相关文章
相关标签/搜索