Skip to content Skip to sidebar Skip to footer

How To Change Fragment Inside A Dialogfragment

I want to make an empty DialogFragment with a LinearLayout and then change fragments inside the LinearLayout. For example, a login where the first fragment is 3 button (facebook, g

Solution 1:

You need to get the childFragmentManager from the dialogfragment link, then from the child fragments you can change the fragments via getParentFragment().getChildFragmentManager()

Solution 2:

I found a better way than getParentFragment. You can in parent fragment add public static FragmentManager and use it in nested fragment:

In parent:

publicclassSelectProductDialogFragmentextendsDialogFragment {
publicstatic FragmentManager fragmentManager;

@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Viewv= inflater.inflate(R.layout.dialog_add_product,container,false);
    fragmentManager = getChildFragmentManager();
    return v;
}
}

In child: for example for replace transaction:

ProductsGridFragmentproductsGridFragment=newProductsGridFragment();
                    FragmentTransactiontransaction= SelectProductDialogFragment.fragmentManager.beginTransaction()
                            .replace(R.id.dialog_order_container,productsGridFragment)
                            .addToBackStack(null);

                    transaction.commit();

Post a Comment for "How To Change Fragment Inside A Dialogfragment"