使用AppCompatDialogFragment 或者 DialogFragment 的過程中遇到java.lang.IllegalStateException: Fragment already added: 的解決方法:
private CountryChooseDialog mCountryChooseDialog; private void showCountryChooseDialog() { if (mCountryChooseDialog == null) { mCountryChooseDialog = CountryChooseDialog.newInstance(); } if (mCountryChooseDialog.isAdded()) { //解決方法就是添加這行代碼,如果已經添加了,就移除掉然后再show,就不會出現Fragment already added的錯誤了。 getSupportFragmentManager().beginTransaction().remove(mCountryChooseDialog).commit(); } mCountryChooseDialog.show(getSupportFragmentManager(), mCountryChooseDialog.getClass().getName()); }
