1. 程式人生 > >java.lang.IllegalStateException: Fragment already added異常的處理

java.lang.IllegalStateException: Fragment already added異常的處理

先判斷是否有fragment,如果fragment不為null,則判斷是都新增進去了,如果新增過直接展示不要重複建立
StaticFragment staticFragment = new StaticFragment();
if (staticFragment != null) {
    if (staticFragment.isAdded()) {
        getSupportFragmentManager().beginTransaction().show(staticFragment)
                .commit();
    } 
} else {
    getSupportFragmentManager().beginTransaction().add(R.id.bt_frameLayout, staticFragment, "STATIC_FRAGMENT")
            .commit();
}