How to replace a DialogFragment by another DialogFragment?
Asked Answered
L

4

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?

Lavation answered 30/3, 2016 at 10:4 Comment(2)
I'm guessing the only possible way is to dismiss current DialogFragment and show new one.Upside
please visit this #25516619Homologate
C
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");
}
Cloistered answered 30/11, 2018 at 20:22 Comment(0)
B
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.

Brewster answered 15/2, 2017 at 0:34 Comment(0)
T
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!" );
        }
    }
}
Turney answered 11/3, 2017 at 9:11 Comment(0)
O
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.

Odelsting answered 27/8, 2019 at 20:56 Comment(1)
Firstly it would be great to leave a comment as well rather than just downvote an answer. Secondly the answer is edited for those interestedOdelsting

© 2022 - 2024 — McMap. All rights reserved.