Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a DialogFragment by another DialogFragment?

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?

like image 453
Solace Avatar asked Mar 30 '16 10:03

Solace


People also ask

What is a dialogfragment?

Essentially a DialogFragment displays a Dialog but inside a Fragment. Google recommends that we use DialogFragment instead of a simple Alert Dialog builder in the activity. Why so? DialogFragments have their own lifecycle methods. So the Activity is free from the responsibility of telling the Dialog what to do.

How do I retrieve a dialogfragment from a fragment?

When creating a DialogFragment from within a Fragment, you must use the Fragment 's child FragmentManager to ensure that the state is properly restored after configuration changes. A non-null tag allows you to use findFragmentByTag () to retrieve the DialogFragment at a later time. // PurchaseConfirmationDialogFragment.

How do I dismiss a dialogfragment?

DialogFragment also contains methods to dismiss or set the cancellability of your DialogFragment: dismiss () - Dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this entry are popped.

How to extend from bottomsheetdialogfragment instead of dialogfragment?

Instead of DialogFragment, you can extend from BottomSheetDialogFragment: When you show this fragment, you will notice that it appears from the bottom: Notice that we are using the support library version of fragments for better compatibility in our code samples. The non-support version works identically.


2 Answers

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.

like image 148
Hanan Rofe Haim Avatar answered Oct 16 '22 03:10

Hanan Rofe Haim


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");
}
like image 23
Vitaly Zinchenko Avatar answered Oct 16 '22 02:10

Vitaly Zinchenko