前言
公司項(xiàng)目在完結(jié)時(shí)遇到一個(gè)問題,用正式簽名打包后進(jìn)行測試發(fā)現(xiàn),在程序正常運(yùn)行時(shí)按下home鍵返回桌面,此時(shí)再點(diǎn)擊APP的icon并不會(huì)回到之前的狀態(tài),而是會(huì)從splash頁開始重啟程序,而從任務(wù)列表里打開APP則無此問題,在debug簽名下也并沒有遇到這個(gè)問題.后來查了查據(jù)說這是studio打包的bug.解決方法如下
在你的啟動(dòng)頁加入如下代碼:
if (!isTaskRoot()) {
finish();
return;
}
isTaskRoot()是判斷當(dāng)前activity是否是當(dāng)前任務(wù)棧中的最早的(最后一個(gè))activity,是返回true,否則返回false.
我的理解是如果第一頁不是任務(wù)棧中的最初的activity則說明任務(wù)棧之前已經(jīng)存在,則finish掉當(dāng)前頁并return,此時(shí)則顯示出當(dāng)前任務(wù)棧最上層的activity,如果理解有誤,還請各位朋友評論中指導(dǎo)下~~.
這是isTaskRoot()方法及注釋
/**
* Return whether this activity is the root of a task. The root is the
* first activity in a task.
*
* @return True if this is the root activity, else false.
*/
public boolean isTaskRoot() {
try {
return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0;
} catch (RemoteException e) {
return false;
}
}