欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android App应用启动分析与优化

程序员文章站 2024-03-06 00:01:37
app的启动方式:  1.)冷启动  当启动应用时,后台没有该应用的进程,这时系统会重新创建一个新的进程分配给该应用,这个启动方式就是冷启动。冷启动...

app的启动方式:
 1.)冷启
 当启动应用时,后台没有该应用的进程,这时系统会重新创建一个新的进程分配给该应用,这个启动方式就是冷启动。冷启动因为系统会重新创建一个新的进程分配给它,所以会先创建和初始化application类,再创建和初始化mainactivity类(包括一系列的测量、布局、绘制),最后显示在界面上。
 2.)热启动 
当启动应用时,后台已有该应用的进程(例:按back键、home键,应用虽然会退出,但是该应用的进程是依然会保留在后台,可进入任务列表查看),所以在已有进程的情况下,这种启动会从已有的进程中来启动应用,这个方式叫热启动。热启动因为会从已有的进程中来启动,所以热启动就不会走application这步了,而是直接走mainactivity(包括一系列的测量、布局、绘制),所以热启动的过程只需要创建和初始化一个mainactivity就行了,而不必创建和初始化application,因为一个应用从新进程的创建到进程的销毁,application只会初始化一次。 

app的启动流程: 
通过上面的两种启动方式可以看出app启动流程为: 
application的构造器方法——>attachbasecontext()——>oncreate()——>activity的构造方法——>oncreate()——>配置主题中背景等属性——>onstart()——>onresume()——>测量布局绘制显示在界面上

app的启动优化:
基于上面的启动流程我们尽量做到如下几点
 1.application的创建过程中尽量少的进行耗时操作
 2.如果用到sharepreference,尽量在异步线程中操作
 3.减少布局的层次,并且生命周期回调的方法中尽量减少耗时的操作

app启动遇见黑屏或者白屏问题
1.)产生原因 
其实显示黑屏或者白屏实属正常,这是因为还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。
 示例:

 Android App应用启动分析与优化

2.)解决办法
通过设置设置style 
(1)设置背景图theme 
通过设置一张背景图。 当程序启动时,首先显示这张背景图,避免出现黑屏

<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> 

解决后示例:

 Android App应用启动分析与优化

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" //平板风格显示
干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。