《Android之 部分系統安裝器bug導致應用重啟解決方法》
轉載請注明來自 傻小孩b_移動開發(http://www.lxweimin.com/users/d388bcf9c4d3)喜歡的可以關注我,不定期總結文章!您的支持是我的動力哈!
當第一次應用安裝的時候,選擇打開的時候,當我們進入應用后,按住Home鍵切回桌面,然后才重新進入應用,會發現應用會重新啟動。
初次判斷是部分手機系統安裝器的bug,即使我們在AndroidManifest的配置設置了啟動頁模式,如下:
<activity
android:name=".ui.splashActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/myTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
但是通過安裝器完成安裝后,點擊“打開”后啟動的應用,啟動頁Activity不會持有AndroidManifest配置的action和category。目前還找不到比較權威性的解釋。
目前解決方法
解決方法思路很簡單:首先在首次啟動的activity(設置action時MAIN的activity),在創建前(oncreate())先判斷當前的activity,intent是否持有配置的action和category,沒有的話就finish后在startActivity同個activity實例出來。如下代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
//FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 可以表示是否從桌面進來
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
// 如果沒有檢測到配置的action和category, finsh -> start
finish();
Intent mIntent = new Intent(this, splashActivity.class);
mIntent.setAction(Intent.ACTION_MAIN);
mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
}
super.onCreate(savedInstanceState);
}
目前這種方法可以解決,如果有讀者大神知道什么原因導致的,歡迎聯系交流哈~
傻小孩b mark共勉,寫給在成長路上奮斗的你