Reputation: 73
I have been battling with this for some days now.
Below is a snippet of my code for FragmentB:
public class FragmentB extends Fragment {
//...Some code....
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.new_order_create_fragment, container, false);
}
void showDialog(int position, Product item){
MyDialogFragment dialogFragment=new MyDialogFragment();
dialogFragment.show(getChildFragmentManager(), "MY_DIALOG_FRAG");
}
//...More Code....
}
PS: showDialog(int position, Product item)
is used to show the dilogFragment.
I'm using getChildFragmentManager()
But I have also tried getFragmentManager()
This the code for MyDialogFragment:
public class MyDialogFragment extends AppCompatDialogFragment {
//...Some code....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(AppCompatDialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.edit_cart_item_layout, container, false);
}
//...More code....
}
Thank you.
Upvotes: 1
Views: 671
Reputation: 204886
Like other fragments, you can attach a DialogFragment to a particular container. For example, if you had a <FrameLayout android:id="@+id/container"/>
in your Fragment B, you can use
void showDialog(int position, Product item) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
MyDialogFragment dialogFragment = new MyDialogFragment();
ft.add(R.id.container, dialogFragment, "MY_DIALOG_FRAG");
ft.commit();
}
Upvotes: 1