O
O
orbit0702019-08-13 15:31:23
Android
orbit070, 2019-08-13 15:31:23

Where does the fragment go?

I have a regular DialogFragment to show a ProgressBar

public class LoadingDialogFragment extends DialogFragment {
    public static final String TAG = "LoadingDialogFragment";

    public static LoadingDialogFragment newInstance() {
        return new LoadingDialogFragment();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.loading_indicator);

        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        return dialog;
    }
}

I show and hide this indicator like this
protected void showLoadingDialogFragment() {
        LoadingDialogFragment dialog = LoadingDialogFragment.newInstance();
        dialog.setCancelable(false);
        dialog.show(getSupportFragmentManager(), LoadingDialogFragment.TAG);
    }

    protected void hideLoadingDialogFragment() {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.TAG); // здесь null
        if (fragment instanceof LoadingDialogFragment) {
            ((DialogFragment) fragment).dismiss();
        }
    }

Everything has always worked fine, but recently a situation arose in one place: the dialog is displayed, but after the data is loaded, it is not hidden. I use a debugger to see what and how - the hideLoadingDialogFragment method is called, but
getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.TAG);
returns null (I indicated this place in the code), despite the fact that this very dialog is displayed on the screen. How can you catch the error? what am I doing wrong, where does the fragment from the FragmentManager disappear to?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Vasilenko, 2019-08-22
@farewell

Of course, "but after the end of the data loading" is a very weak clue and there are a lot of options for the turn of events.
I would venture to guess that the FragmentManager that returns null is no longer the instance that existed when the dialog was launched. This happens, for example, as a result of changing the orientation of the device.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question