Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Fragment from a DialogFragment (replacing the Dialogs parent)

Let's say I have Fragment A, from which I open a DialogFragment like this:

FragmentActivity fragmentActivity = (FragmentActivity) view.getContext();
FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
Fragment prev = fragmentActivity.getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
  ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment fragmentDialog = MyDialogFragment.newInstance();
fragmentDialog.show(ft, "dialog");

From this Dialog, after clicking (positive / neutral / negative) button, I want to open Fragment B, which should replace Fragment A.

In the Dialog's onClick method I run a callback method of parent Activity:

@Override
public void onClick(DialogInterface dialog, int which) {
  switch(which) {
    case DialogInterface.BUTTON_NEUTRAL:
      detailsCallbacks.openMoreDetails(); 
      break;
  }
}

And finally my Activity's openMoreDetails() method looks like this:

@Override
public void openMoreDetails() {
  Fragment fragmentB = Fragment.newInstance();
  FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  ft.replace(R.id.fragment_container, fragmentB);
  ft.addToBackStack(null);
  ft.commit();
}

What I get is strange. Fragment B blinks on the screen just for a fraction of a second and then is replaced (covered?) by Fragment A again.

When I click the 'up' button I get back from Fragment A, so none of these transactions were added to the back stack. I would like to show Fragment B and then, when pressing the 'up' button, go back to Fragment A.

Is it somehow possible? And what's wrong with my approach?

like image 376
Marcel Bro Avatar asked Feb 01 '13 16:02

Marcel Bro


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is DialogFragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

Which function is used to add fragment as child to view while implementing fragment as a dialog?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is a dialog fragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


2 Answers

Just had the same problem:

Fragment A display a custom dialog fragment.

At click on one of the buttons of the dialog fragment, I wanted to remove the dialog and show Fragment B.

Fragment B was displayed and instantly disappear. My screen was displaying Fragment A again.

What was wrong on my initial implementation:

private void onClickInscription() {
    FragmentInscription frag = FragmentInscription.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.main, frag);
    ft.addToBackStack(null);
    ft.commit();
    dismiss();
}

And the correct one:

private void onClickInscription() {
    dismiss();
    FragmentInscription frag = FragmentInscription.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.main, frag);
    ft.addToBackStack(null);
    ft.commit();
}

So try to call dismiss first on your dialog then apply the FragmentTransction

like image 163
ol_v_er Avatar answered Oct 14 '22 00:10

ol_v_er


I know this it's been a long time since the problem was posted, but I solved it by adding a if(fragmentB.getView != null) before the replace statement.

It finally doesn't do the flash and disappear thing. (:

like image 37
Prash Avatar answered Oct 14 '22 01:10

Prash