I have a DialogFragment in my App which often makes the application crash cause of:
Fatal Exception: java.lang.IllegalStateException
Can not perform this action after onSaveInstanceState
it.dialogs.LoadingDialog.show (LoadingDialog.java:39)
it.TablesActivity.showLoading (TablesActivity.java:135)
it.TablesActivity.getTable (TablesActivity.java:148)
it.TablesActivity.onClickTable (TablesActivity.java:415)
it.fragments.TablesFragment$4.onResponse
(TablesFragment.java:300)
My DialogFragment looks like this:
public class LoadingDialog extends DialogFragment {
public static String TAG = "LoadingDialog";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.alert_loading, container);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().setCancelable(false);
getDialog().setCanceledOnTouchOutside(false);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
@Override
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
if (manager.findFragmentByTag(LoadingDialog.TAG) == null) {
super.show(manager, tag); // line 39
}
}
}
And in my Activity the showLoading method is the following:
public void showLoading(boolean show) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(LoadingDialog.TAG);
if (fragment instanceof LoadingDialog) {
((LoadingDialog) fragment).dismissAllowingStateLoss();
} else if (show) {
new LoadingDialog().show(getSupportFragmentManager(), LoadingDialog.TAG);
}
}
How can I prevent that type of crash in my application?