Android DialogFragment onViewCreated not called

Gaurav Vashisth picture Gaurav Vashisth · May 15, 2012 · Viewed 21k times · Source

I am using android compatibility library (v4 revision 8). In the custom DialogFragment the overrided method onViewCreated is not getting called.For eg.

public class MyDialogFragment extends DialogFragment{
    private String mMessage;
    public MyDialogFragment(String message) {
        mMessage = message;
    }

    @Override
    public Dialog onCreateDialog( Bundle savedInstanceState){
        super.onCreateDialog(savedInstanceState);
        Log.d("TAG", "onCreateDialog");
        setRetainInstance(true); 
        //....do something
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Log.d("TAG", "onViewCreated");
        //...do something
    }
}

onViewCreated is not getting logged.

Answer

Barak picture Barak · May 15, 2012

Well, the docs for onViewCreated state "Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned".

DialogFragment uses onCreateDialog and not onCreateView, so onViewCreated is not fired. (Would be my working theory, I haven't dived into the android source to confirm).