在更新数据和显示 dialogFragment 期间点击主页时崩溃。非法状态异常

Crash when tap Home during update data and show dialogFragment. IllegalStateException

在我的例子中,在设置我的应用程序时,我点击按钮更新数据库,它向我显示带有进度的 dialogFragment,如果我点击主页按钮 - 我的应用程序崩溃。

记录错误:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
                                                                        at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1489)
                                                                        at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1507)
                                                                        at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
                                                                        at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
                                                                        at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:201)
                                                                        at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:167)
                                                                        at com.guardian.glass.ui.settings.SettingsActivity.onComplete(SettingsActivity.java:328)
                                                                        at com.guardian.glass.ui.DialogProgressFragment.onDataLoaded(DialogProgressFragment.java:63)

在我的class SettingsActivity 方法代码中弹出错误。

private void fetchDatabaseFromServer() {
        UserOperationsTask getCountriesTask = new UpdateCountries(new DialogProgressFragment() {
            @Override
            public void onComplete(boolean success) {
                dismiss(); // here i get error
                updateDatabaseSize();
                updateFilters();
                MeasureUtils.makeDefaultTemplate(SettingsActivity.this, true);
            }
        });
        getCountriesTask.execute(SettingsActivity.this);
        showProgress();
        updateDataBaseProfile();
    }

我的内心class

private class UpdateCountries extends UserOperationsTask {
        final DialogProgressFragment progressDialog;

        UpdateCountries(DialogProgressFragment progressDialog) {
            super(UserOperationsTask.TaskMode.MODE_LOAD_COUNTRIES, null);
            this.progressDialog = progressDialog;
        }

        @Override
        public void onLoadFinished(Bundle resultData) {
            setRegionLabels();
            updateFilters();
            hideProgress();
            String errorMessage = resultData.getString(UserOperationsTask.RESULT_ERROR_STRING);
            if (errorMessage != null) {
                Log.d(TAG, "getCountriesFromServer error: " + errorMessage);
                showMessage(getString(R.string.info_title), getString(R.string.server_request_error));
            } else {
                progressDialog.start(SettingsActivity.this, DialogProgressFragment.DialogMode.MODE_UPDATE);
                progressDialog.setCancelable(false);
                progressDialog.show(getSupportFragmentManager(), DialogProgressFragment.class.getSimpleName());
            }
        }
    }

请告诉我如何最好地解决这个问题?

请注意,如果您在 onstop 后更新 ui,应用程序将自动崩溃。 因此,如果您正在异步更新应用程序的 ui,请确保它们是某种检测您的 activity 未处于停止状态的机制。

或者您可以在 dialogfragment class.

中将提交更改为 commitAllowingStateLoss

使用 commitAllowingStateLoss() 而不是 commit()。如果发生状态丢失,它不会抛出异常。

谢谢大家!我找到了解决方案 - 就我而言,而不是

dismiss();

更好地使用

dismissAllowingStateLoss();