How to change a fragment inside a dialog fragment
I want to make it empty DialogFragment
with LinearLayout
and then change fragments inside LinearLayout
. For example a login where the first snippet is 3 buttons (facebook, google +, email login) and when someone clicked email then 2.the snippet has a layout with EditTexts
if google or Facebook was clicked and another snippet appears with ProgressBar
...
this is my empty dialog:
<?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 piece of code (I'm 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(...)
:
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;
}
source to share
I found a better way than getParentFragment
. You can add in the parent fragment public static FragmentManager
and use it in the nested fragment:
In the 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 a child: for example, to replace a transaction:
ProductsGridFragment productsGridFragment = new ProductsGridFragment();
FragmentTransaction transaction = SelectProductDialogFragment.fragmentManager.beginTransaction()
.replace(R.id.dialog_order_container,productsGridFragment)
.addToBackStack(null);
transaction.commit();
source to share