Hope you are doing well.
I created a dialog fragment and called show() on the instance. I passed a custom tag to show()'s parameter. The fragment requires no other arguments. On config change, resizing the window of the app, the app crashes.
Caused by: android.view.InflateException: Binary XML file line #35 in com.signal.android.stage:layout/activity_main2: Binary XML file line #35 in com.signal.android.stage:layout/activity_main2: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #35 in com.signal.android.stage:layout/activity_main2: Error inflating class fragment
Caused by: java.lang.IllegalStateException: DialogFragment 0 doesn't exist in the FragmentManager
at androidx.navigation.fragment.DialogFragmentNavigator.onRestoreState(DialogFragmentNavigator.java:148)
This is a method from the DialogFragmentNavigator.java:
@Override
public void onRestoreState(@Nullable Bundle savedState) {
if (savedState != null) {
mDialogCount = savedState.getInt(KEY_DIALOG_COUNT, 0);
for (int index = 0; index < mDialogCount; index++) {
DialogFragment fragment = (DialogFragment) mFragmentManager
.findFragmentByTag(DIALOG_TAG + index);
if (fragment != null) {
fragment.getLifecycle().addObserver(mObserver);
} else {
throw new IllegalStateException("DialogFragment " + index
+ " doesn't exist in the FragmentManager");
}
}
}
}
Please see that the DIALOG_TAG that has been used has been hard coded to "androidx-nav-fragment:navigator:dialog:". So It makes sense that the Fragment is not found since I gave a custom TAG.
What is the expectation form the clients calling the show() method? What tag should be passed to gracefully restore the fragment?
Stay safe!