I'm getting the following crash reports:
Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1832) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1850) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:643) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:603) at android.support.v4.app.DialogFragment.show(DialogFragment.java:143)
This following code is causing the crash, I cut out some trivial setup code for clarity. I have read up on this error and as I understand it the .show()
should be safe within a user interaction such as onClick()
. The only thing I can think of is that the query()
is taking a long time and the user swaps out. Is that a sensible explanation for this error? It's instantaneous on my devices even with large DB. Any other possibilities? Thanks!
foldersButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
final List<String> paths = new ArrayList<>();
try(Cursor c = getActivity().getContentResolver().query(Meta.CONTENT_URI,
new String[]{"DISTINCT " + Meta.PARENT}, null, null,
Meta.PARENT + " ASC"))
{
while (c != null && c.moveToNext())
{
String path = c.getString(c.getColumnIndex(Meta.PARENT));
// We place the excluded folders at the end
if (!mExcludedFolders.contains(path))
paths.add(path);
}
}
[setup...]
int[] position = new int[2];
foldersButton.getLocationOnScreen(position);
FolderDialog dialog = FolderDialog.newInstance(
paths.toArray(new String[paths.size()]),
visible,
excluded,
position[0],
position[1]);
dialog.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.FolderDialog);
[setup...]
dialog.show(getFragmentManager(), TAG);
}
});
If this code of yours is within an activity, wrap the call to show
with:
if(!isDestroyed() && !isFinishing())
and if this code is within a fragment , wrap the call to show
with:
if (isResumed() && !isRemoving())
This more or less would solve the issue of landing in an inconsistent UI state