Search code examples
androidandroid-studioandroid-fragmentsandroid-dialogfragmentandroid-dialog

Opening another dialog fragment and closing the previous one


In my code I have to open another dialog fragment when a textview is clicked, the textView is in the main dialog Fragment

textView.setOnClickListener(new View.OnclickListener(){
  @Override
  public void onClick(View v){
     NewDialogFragment.newInstance().show(getChildFragmentManager(),"");
     dismiss(); 
   }
});

problem is that, when the textview is clicked in the main dialog fragment gets dismiss() but new DialogFragment doesn't open,

expected result is that the main dialogFrament should dismiss() and the NewDialogFragment() should appear

Thanks


Solution

  • Change getChildFragmentManager() to just getParentFragmentManager().

    The reason for doing this is because you want the FragmentManager of the Activity, not of the DialogFragment you're about to dismiss. Else, you'll give the FragmentManager of the dialog and then you dismiss it which will dismiss the dialog you just attached.

    textView.setOnClickListener(new View.OnclickListener(){
      @Override
      public void onClick(View v){
         NewDialogFragment.newInstance().show(getParentFragmentManager(),"");
         dismiss(); 
       }
    });