10

We add a general/normal Fragment programatically by doing something like:

fragmentTransaction.add(containerViewId, fragmentToAdd, fragmentTag);

and we replace a Fragment by another by doing something like:

fragmentTransaction.replace(containerViewId, newFragment, tagOfNewFragment);

But we add a DialogFragment by

dialogFramentInstance.show(fragmentManager, fragmentTag);

The question is that how should I replace this DialogFragment which has been added by the show() method?

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 4
    I'm guessing the only possible way is to dismiss current `DialogFragment` and show new one. – Wukash Mar 30 '16 at 10:06
  • 1
    please visit this http://stackoverflow.com/questions/25516119/how-to-replace-one-full-screen-dialog-fragment-with-another-without-showing-pare – H Raval Mar 30 '16 at 10:07

4 Answers4

4
private void closeYourDialogFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment fragmentToRemove = getSupportFragmentManager().findFragmentByTag("your_dialog_fragment");
    if (fragmentToRemove != null) {
        ft.remove(fragmentToRemove);
    }
    ft.addToBackStack(null);
    ft.commit(); // or ft.commitAllowingStateLoss()
}

private void replaceYourDialogFragment() {
    closeYourDialogFragment();

    YourDialogFragment yourDialogFragment = new YourDialogFragment();
    yourDialogFragment.show(getSupportFragmentManager(), "your_dialog_fragment");
}
Vitaly Zinchenko
  • 4,871
  • 5
  • 36
  • 52
3
dialogFramentInstance.show(fragmentManager, fragmentTag);

Just adds the dialog fragment to the fragment manger using an add transaction (with no container).

In order to replace fragments you'll need a container and since you don't have one your only option is to dismiss() the first one and show() the new one.

Hanan Rofe Haim
  • 870
  • 6
  • 11
1

Maybe you can do like this:

    public void showFragment(Fragment fragment) {
    if (fragment instanceof DialogFragment) {
        FragmentTransaction ft = mContext.getFragmentManager().beginTransaction();
        Fragment prev = mContext.getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            Log.d(TAG, "showFragment: remove prev...." + prev.getClass().getSimpleName());
            ft.remove(prev);
        }
        mContext.getFragmentManager().executePendingTransactions();
        if (!fragment.isAdded()){
            ft.addToBackStack(null);
            ((DialogFragment) fragment).show(ft, "dialog");
        } else {
            Log.w(TAG, "showFragment: fragment has been added!" );
        }
    }
}
zhuzp
  • 11
  • 2
0

So this took me a lot of digging to figure out.

Dialog fragment show method only adds fragments hence if you want to replace them you have to manually remove the previous dialog fragment.

One thing to keep in mind, it is important to use the same fragmentManager used to open the initial dialog fragment. For example if you opened the first dialog fragment via an Activity (supportFragmentManager) and now using the dialog fragment fragment manager (childFragmentManager) since they do not have the same stack you wont be able to access the original dialog fragment and remove it.

  • Firstly it would be great to leave a comment as well rather than just downvote an answer. Secondly the answer is edited for those interested – user1808592 Sep 10 '19 at 20:27