Android - IllegalStateException: Can not perform this action after onSaveInstanceState

摘要:Android - IllegalStateException: Can not perform this action after onSaveInstanceState

今天出現了一個bug,幸好是在我連接USB狀態下發生的錯誤,

後來查知,是 IllegalStateException: Can not perform this action after onSaveInstanceState這個問題

很容易就找到原因是在

    public void replaceFragment(Fragment frag) {
        if (frag != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frag, frag)
                    .commit();
        } 
    }
 
只要將commit();
改為commitAllowingStateLoss();
 
雖然不是很懂
但參考了一下兩篇文章
http://blog.csdn.net/ranxiedao/article/details/8214936
 
http://blog.csdn.net/stoppig/article/details/31776607
 
應該會略懂為什麼。(雖然還是不懂,誤)