Search code examples
androidfragmentdialogfragmentfragmentmanager

Failed to execute DialogFragment in Fragment


When I try to execute the DialogoFragment in a Fragment, it tells me to create the Show method and I already tried with: this, getContext (), getFragmenteManager (), getActivity (). GetFragmenteManager (). In the Activity it does not give me problems to execute it only in the Fragment.

I am running this code from a Fragment:

public void onActivityCreated(Bundle savedInstanceState) {
 btnComentario = (Button) getView().findViewById(R.id.btnComentario);
 btnComentario.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    super.onActivityCreated(savedInstanceState);
      (new MyDialogFragment()).show(getActivity().getFragmentManager(), "DialogFrament");
      }
    });

}

Thanks for the help


Solution

  •  @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
     // TODO Auto-generated method stub
            super.onViewCreated(view, savedInstanceState);
     btnComentario = (Button) getView().findViewById(R.id.btnComentario);
     btnComentario.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
         FragmentManager fm = getFragmentManager();
                MyDialogFragment dialogFragment = new MyDialogFragment();
                dialogFragment.show(fm,"");
          }
        });
    }
    

    Try this method on onViewCreated inside fragment. It's working solution. If it works fine kindly Tick the answer so as to pass info for researchers.