了解Activity的生命周期很久了,最近在网上学习了一下Activity的启动过程,和程序的启动过程,想知道当手机开机以后,手机进入到主页面,点击一个程序,这个程序是怎么启动的,在程序内部,Activity又是怎么跳转的,里面的东西太多,也只能了解个大概,然后写一下笔记:
在Android系统中,Launcher程序就是我们平时看到的桌面程序,它也是一个android程序,只不过这个应用程序是默认系统的第一个启动的应用程序,这里简单分析一下Launcher应用的启动流程。
不同的手机厂商定制android操作系统的时候都会更改Launcher的源代码,我们这里以android23的源码为例大致的分析一下Launcher的启动流程。
![这里写图片描述](https://img-blog.csdn.net/20160721150934177)
这张图很熟悉了吧,这不是就是eclipse中app项目的结构吗,里面点开AndroidManifest:
<application
android:name="com.android.launcher2.LauncherApplication"
...
>
<activity
android:name="com.android.launcher2.Launcher"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@style/Theme"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
此AndroidManifest正式android手机开机以后的启动程序配置文件,从中我们可以知道启动过程需要先后初始化LauncherApplication和Launcher的对象。更加简洁的说,启动过程可以分成两步,第一步在LauncherApplication.onCreate()方法中,第二部在Launcher.onCreate()方法中。
先来看LauncherApplication的代码:
public class LauncherApplication extends Application {
public void onCreate() {
super.onCreate();
final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
sIsScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
sScreenDensity = getResources().getDisplayMetrics().density;
mIconCache = new IconCache(this);
mModel = new LauncherModel(