Search code examples
androidandroid-dialogfragmentandroid-8.0-oreo

Android O FragmentManager is null


I'm testing this on an Emulator with the Android O Developer Preview. On previous versions everything works fine.

I got a LoginFragment which shows a "Please Wait" progress dialog when the login is processing.

public class ProgressDialogHud extends DialogFragment {

    private String  messages;

    public static ProgressDialogHud newInstance(String message) {
        ProgressDialogHud dialog = new ProgressDialogHud();
        // ...
        return Dialog;
    }
}

public class LoginFragment extends Fragment {
    private DialogFragment mProgressDialog;

    private void login() {
        mProgressDialog = ProgressDialogHud.newInstance( "..." );
        mProgressDialog.show( getActivity().getSupportFragmentManager(), "PROGRESS" );
    }

    private void onLoginFinished() {
        mProgressDialog.dismiss(); // NullPointerException here because inside Fragment (DialogFragment extends Fragment) the FragmentManager is null
    }
}

I did a litle debugging session and found that the DialogFragment uses 2 different FragmentManager for showing and hiding. On showing the fragment, the manager is not null, however it's null when it's hiding.

Here is the Stacktrace

Any ideas?


Solution

  • You were close to the answer already: good investigation.

    Use getFragmentManager() instead of getActivity().getSupportFragmentManager() when showing the dialog.