一、問題描述
當(dāng)安裝新應(yīng)用時(shí),安裝完成后會(huì)出現(xiàn)一個(gè)“打開”和“完成”界面,點(diǎn)擊完成沒有問題,但是點(diǎn)擊“打開”就出現(xiàn)了問題。
具體操作如下:
step1:打開應(yīng)用收到更新提示(啟動(dòng)頁檢查更新),點(diǎn)擊“立刻更新”
setp2:下載成功后點(diǎn)擊“安裝”,
setp3:安裝成功后出現(xiàn)“打開”和“完成”界面,點(diǎn)擊“打開”,應(yīng)用先顯示啟動(dòng)頁面然后進(jìn)入到主界面,
setp4:按下HOME鍵,回到桌面,然后點(diǎn)擊桌面應(yīng)用圖標(biāo),會(huì)先顯示啟動(dòng)頁面然后再顯示主界面。
二、問題原因
剛開始還以為是應(yīng)用崩潰了,才重新打開了啟動(dòng)界面,后面經(jīng)過測試和日志發(fā)現(xiàn)并沒有任何的異常。What?遇見鬼了!經(jīng)過搜索終于發(fā)現(xiàn)了問題的原因:
This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. 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.
原來這三種打開方式是有區(qū)別的,通過idea安裝打開應(yīng)用的intent是沒有action和category的,桌面點(diǎn)擊圖標(biāo)打開應(yīng)用的intent是帶有action(android.intent.action.MAIN) 和category(android.intent.category.LAUNCHER)的,而按照器打開應(yīng)用只帶有action(android.intent.action.MAIN)而沒有category。
二、解決方式
再啟動(dòng)界面的onCreate添加方法添加如下代碼
@Override
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
? ? ? ? // Activity was brought to front and not created,
? ? ? ? // Thus finishing this will get us to the last viewed activity
? ? ? ? finish();
? ? ? ? return;
? ? }
? ?......
}
三、參考
https://stackoverflow.com/questions/6337217/how-to-return-to-the-latest-launched-activity-when-re-launching-application-afte
http://www.itdecent.cn/p/eea14ca0b164
https://developer.android.google.cn/guide/components/tasks-and-back-stack