問(wèn)題
項(xiàng)目發(fā)版當(dāng)晚發(fā)現(xiàn)的:安裝完APP后直接點(diǎn)擊打開,接著Home鍵退到后臺(tái),再次打開App 時(shí)發(fā)現(xiàn)重啟了。試了一下去哪兒,陌陌等幾個(gè)主流app,也有這個(gè)問(wèn)題
原因
The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.
大概就是桌面啟動(dòng)app和安裝器啟動(dòng)app的Intent不一樣,安裝完直接open其實(shí)已經(jīng)啟動(dòng)了app,但是桌面點(diǎn)擊圖標(biāo)沒(méi)有認(rèn)為你已經(jīng)啟動(dòng)了app,于是重啟。
解決辦法
在你的app的入口Activity的onCreate()方法最開始加入代碼段:
if (!this.isTaskRoot()) { //判斷該Activity是不是任務(wù)空間的源Activity,false也就是說(shuō)是被系統(tǒng)重新實(shí)例化出來(lái)
//如果你就放在launcher Activity中話,這里可以直接return了
Intent mainIntent = getIntent();
String action = mainIntent.getAction();
if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
finish();
return;//finish()之后該活動(dòng)會(huì)繼續(xù)執(zhí)行后面的代碼,加return避免可能的exception
}
}