Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, google+, email login) and when somebody pressed email then the 2. fragment has a layout with EditTexts if Google or Facebook was pressed then the other fragment appears with a ProgressBar.

this is my empty dialog layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/dialog_background">

    <LinearLayout
        android:id="@+id/testFragmentController"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="15dp"></LinearLayout>
</RelativeLayout>

And this is the first fragment's code (I am using android annotations):

@EFragment(R.layout.dialog)
public class FragmentGeneralDialog extends ClickDialogFragment {

    @ViewById
    LinearLayout testFragmentController;


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        this.setStyle(R.style.Dialog_No_Border, getTheme());

        return dialog;
    }


    @AfterViews
    void afterViews() {
        loadActivity();
        GlobalData.setFragmentContainer(testFragmentController);
        activity.loadMenuFragment(FragmentSocialDialog_.class, new SlideLeftAnimation());

    }


}

loadMenuFragments(...) is this:

public <T extends Fragment> T loadMenuFragment(final Class<T> cls,
                                                   final IAnimation a) {
        T fragment = null;
        try {
            fragment = cls.newInstance();
        } catch (Exception ex) {
            throw new RuntimeException("Fragment " + cls.toString()
                    + " has no empty constructor");
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (a != null) {
            transaction.setCustomAnimations(a.getInId(), a.getOutId(), a.getInId(),
                    a.getOutId());
        }

        transaction.replace(R.id.testFragmentController, fragment);

        try {
            transaction.commit();
        } catch (Exception e) {
        }
        return fragment;
    }
like image 525
MegaX Avatar asked Feb 13 '23 04:02

MegaX


2 Answers

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

like image 67
Rothens Avatar answered Feb 15 '23 09:02

Rothens


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

In parent:

public class SelectProductDialogFragment extends DialogFragment {
public static FragmentManager fragmentManager;

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

In child: for example for replace transaction:

ProductsGridFragment productsGridFragment = new ProductsGridFragment();
                    FragmentTransaction transaction = SelectProductDialogFragment.fragmentManager.beginTransaction()
                            .replace(R.id.dialog_order_container,productsGridFragment)
                            .addToBackStack(null);

                    transaction.commit();
like image 26
Sadeq Shajary Avatar answered Feb 15 '23 11:02

Sadeq Shajary