当我们从Home点击ShortCut图标启动一个应用程序后,这个应用程序打开了很多个Activity,假设顺序为A,B,C,然后我们按Home键,再次从桌面用图标启动这个应用程序,我们会发现显示的是刚才的C,而不是A。这里我们普遍的想法是按Home键是让程序退到后台,然后让桌面显示出来。那么我们就来看看Home键到底是怎么回事。
在Framework中我们找到源码,我们首先在interceptKeyBeforeDispatching这个方法中找到Home按键代码如下
[java]
// If the HOME button is currently being held, then we do special
// chording with it.
if(mHomePressed) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if(keyCode == KeyEvent.KEYCODE_HOME) {
if(!down) {
mHomePressed =false;
if(!canceled) {
// If an incoming call is ringing, HOME is totally disabled.
// (The user is already on the InCallScreen at this point,
// and his ONLY options are to answer or reject the call.)
booleanincomingRinging =false;
try{
ITelephony phoneServ = getPhoneInterface();
if(phoneServ !=null) {
incomingRinging = phoneServ.isRinging();
}else{
Log.w(TAG,"Unable to find ITelephony interface");
}
}catch(RemoteException ex) {
Log.w(TAG,"RemoteException from getPhoneInterface()", ex);
}
if(incomingRinging) {
Log.i(TAG,"Ignoring HOME; there's a ringing incoming call.");
}else{
launchHomeFromHotKey();
}
}else{
Log.i(TAG,"Ignoring HOME; event canceled.");
}
}
}
returntrue;
}这里就是按Home键时执行的方法我们找到
[java]
launchHomeFromHotKey();
/**
* A home key -> launch home action was detected. Take the appropriate action
* given the situation with the keyguard.
*/
voidlaunchHomeFromHotKey() {
if(mKeyguardMediator.isShowingAndNotHidden()) {
// don't launch home if keyguard showing
}elseif(!mHideLockScreen && mKeyguardMediator.isInputRestricted()) {
// when in keyguard restricted mode, must first verify unlock
// before launching home
mKeyguardMediator.verifyUnlock(newOnKeyguardExitResult() {
publicvoidonKeyguardExitResult(booleansuccess) {
if(success) {
try{
ActivityManagerNative.getDefault().stopAppSwitches();
}catch(RemoteException e) {
}
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
startDockOrHome();
}
}
});
}else{
// no keyguard stuff to worry about, just launch home!
try{
ActivityManagerNative.getDefault().stopAppSwitches();
}catch(RemoteException e) {
}
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);
startDockOrHome();
}
}再进入startDockOrHome();
[java]
voidstartDockOrHome() {
Intent dock = createHomeDockIntent();
if(dock !=null) {
try{
mContext.startActivity(dock);
return;
}catch(ActivityNotFoundException e) {
}
}
mContext.startActivity(mHomeIntent);
}这里我们发现,源码中做了车载模式和普通模式Home的区别,在Android2.3原生系统中有车载模式这个应用程式,打开后按Home键我们返回的是车载模式的桌面,而普通情况下按Home返回正常桌面,再来看看mContext.startActivity(mHomeIntent)这个方法,我们发现,Home键也是打开一个activity,不同的是下面的代码
[java]
mHomeIntent =newIntent(Intent.ACTION_MAIN,null);
mHomeIntent.addCategory(Intent.CATEGORY_HOME);
mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);这段代码是在init()方法中,对mHomeIntent的初始化,这个Intent就是跳转到Launcher程序中的配置了 这个属性的Launcher Activity,另外我们还看到两个flag
[java]
FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
newTask保证了我们回到之前Launcher所在的栈,reset task if need是设置清理栈标志,保证清除掉该栈除Launcher以外其他activity(在Launcher清单文件中我们还看到android:clearTaskOnLaunch="true"这个属性,后面会描述),这样我们的Launcher当然会在自己单独的Task中,而且android:launchMode="singleTask"这个属性保证不会启动一个新的Launcher。
通过Launcher启动的其他Activity不会跑到Launcher所在的Task中(后面的文章会提到Launcher启动activity的过程)。所以按Home键是打开了Launcher这个activity并且保证他在自己单独的Task中,其他的activity进入stop状态。
另外Launcher中打开一个activity直接进入之前打开的Task 而不是新打开一个activity.