Search code examples
androidcrashillegalstateexception

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


In my case, in settings my app, I tap to button for update db and it show me dialogFragment with progress, and if I tap Home-button - my app crash.

Log errors:

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$6.onComplete(SettingsActivity.java:328)
                                                                        at com.guardian.glass.ui.DialogProgressFragment$1.onDataLoaded(DialogProgressFragment.java:63)

In my class SettingsActivity code of method where pops up error.

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();
    }

My inner 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());
            }
        }
    }

Tell me please how best to solve this problem?


Solution

  • Thanks guys! I finded solution - in my case, instead

    dismiss();
    

    better use

    dismissAllowingStateLoss();