問(wèn)題
項(xiàng)目發(fā)版當(dāng)晚發(fā)現(xiàn)的:安裝完APP后直接點(diǎn)擊打開(kāi)场钉,接著Home鍵退到后臺(tái),再次打開(kā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的入口A(yíng)ctivity的onCreate()方法最開(kāi)始加入代碼段:
if (!this.isTaskRoot()) { //判斷該Activity是不是任務(wù)空間的源Activity梗逮,false也就是說(shuō)是被系統(tǒng)重新實(shí)例化出來(lái)
//如果你就放在launcher Activity中話(huà),這里可以直接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
}
}