使用apk A 唤起 apk B时 使用PackageManager进行唤醒操作
PackageManager packageManager = getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage("com.xxx.xxx");
if(intent != null) {
startActivity(intent);
}
apk B发生闪退,log显示
01-01 02:42:40.418 9356 9356 E AndroidRuntime: java.lang.IllegalStateException: Failure saving state: active fragment2{866132d (3063406e-5e0f-4ddc-9b3b-763c96d930c1) id=0x7f0a015d android:switcher:2131362141:1} was removed from the FragmentManager
01-01 02:42:40.418 9356 9356 E AndroidRuntime: at androidx.fragment.app.FragmentManagerImpl.saveAllState(FragmentManagerImpl.java:2337)
01-01 02:42:40.418 9356 9356 E AndroidRuntime: at androidx.fragment.app.FragmentController.saveAllState(FragmentController.java:151)
01-01 02:42:40.418 9356 9356 E AndroidRuntime: at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:513)
01-01 02:42:40.418 9356 9356 E AndroidRuntime: at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:533)
01-01 02:42:40.418 9356 9356 E AndroidRuntime: at android.app.Activity.performSaveInstanceState(Activity.java:1549)
经分析,原因在于apk B的Laucher Activity中fragment出现了某些问题导致的。通过报错信息一路跟踪发现起始操作在于onCreate方法的
super.onCreate(savedInstanceState);
一路跟踪至FragmentManagerImpl 2394行。猜测原因在于Intent调起apk时发生了一些问题。
最终通过换了一种调起apk方式解决问题
Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.xxx.xxx", "com.xxx.xxx.xxx"); intent.setComponent(componentName); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);