ProcessPhoenix可以用來重啟android應用,并且速度非??臁2贿^使用中的一些坑需要注意一下
1、重啟時,如果自定義了繼承自Application的子類,該子類會被調用兩次,會導致onCreate被調用兩次,并且這兩次調用是完全相互獨立的,如果你的應用有一些初始化是放在onCreate方法中,并且多次調用可能會出問題。
解決方案1:
推薦使用解決方案1,ProcessPhoenix提供了一個isPhoenixProcess方法用于判斷當前進程是否是臨時的Phoenix進程,可在Application子類的onCreate方法中進行判斷
public void onCreate() {
super.onCreate();
Context context = this.getApplicationContext();
if (ProcessPhoenix.isPhoenixProcess(context)){
return;
}
}
解決方案2:在build.gradle引用ProcessPhoenix,然后在Application子類的onCreate方法中調用通過當前進程名稱來判斷是ProcessPhoenix啟動的進程,還是應用啟動的進程。注:如果在項目中直接拖入ProcessPhoenix.java或者將ProcessPhoenix.java打jar包再加入工程,重啟時啟動的兩個進程名相同,如都為com.example.yangzijiang.activitytest,這樣就無法區分到底是ProcessPhoenix的進程還是應用本身啟動的進程。
public void onCreate() {
super.onCreate();
if ("com.example.yangzijiang.activitytest".equals(getProcessName())){
// 初始化操作可以放在此
}
}
2、啟動Activity還要配置android.intent.category.DEFAULT,否則在ProcessPhoenix.java的getRestartIntent方法中找不到默認要啟動的Activity。
<activity android:name="com.example.yangzijiang.app.RestartAppActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
參考文獻
1.https://github.com/JakeWharton/ProcessPhoenix
2.https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app