Shubham Khare
Shubham Khare

Reputation: 153

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

Upvotes: 4

Views: 2446

Answers (2)

Tim Kist
Tim Kist

Reputation: 1182

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(); 
   }
});

Upvotes: 5

Aj 27
Aj 27

Reputation: 2427

try this:

textView.setOnClickListener(s->{
NewDialogFragment.newInstance().dismiss()
NewDialogFragment.newInstance().show(getChildFragmentManager(),"");
});

Upvotes: 0

Related Questions