Show DialogFragment from another DialogFragment

前端 未结 9 723
春和景丽
春和景丽 2021-02-05 10:33

I have a DialogFragment that displays a list of options to the user, one of these options is \"Delete\" option, when the user presses the delete option I want to sh

相关标签:
9条回答
  • 2021-02-05 10:53

    Very recently, I had this problem and none of the options above worked for me. I tried using the method below:

    DialogFragment fragment = new MyFragment(); //where MyFragment is my fragment I want to show
    fragment.setCancelable(true);
    fragment.show(getSupportFragmentManager(), "timePicker");
    

    This will ONLY work if you're using this in an activity (i.e to call a dialog fragment from an activity class).

    I however fixed this by downcasting my activity instance to an AppCompat activity and using it to call getSupportFragment() as shown below:

    DialogFragment timeFragment = new TimePicker();
    timeFragment.setCancelable(true);
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    timeFragment.show(activity.getSupportFragmentManager(), "timePicker");
    

    I hope this helps.. Merry coding!!

    0 讨论(0)
  • 2021-02-05 10:53

    You can call a DialogFragment from Another DialogFragment.

    NewDialogFragment newDialogFragment= new NewDialogFragment();
    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
    newDialogFragment.show(transaction, "New_Dialog_Fragment");
    
    0 讨论(0)
  • 2021-02-05 11:02

    I got the exact same problem, this situation does not happen when you try to open a DialogFragment from a Fragment.

    The only solution I found was to modify the following call:

    fragment.show(ft, fragmentTag);
    

    To:

    fragment.show(getFragmentManager(), fragmentTag);
    

    The problem with this solution is that we cannot work on the FragmentTransition.

    I don't understand why the behavior is different than with the fragments.

    0 讨论(0)
  • 2021-02-05 11:02

    Please check this following code. Hope this will help many of you!

    public class SubcategoryFragment extends DialogFragment {
    
    public SubcategoryFragment() {
    
    }
    
    public static SubcategoryFragment newInstance(Integer code, String name) {
        SubcategoryFragment fragment = new SubcategoryFragment();
    
        mCode = code;
        mTitle = name;
        return fragment;
    }
    
    
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        liststring = new ArrayList<>();
    
        getAdapter();
    
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_subcategory, container, false);
        gridView = (GridView) view.findViewById(R.id.sub_grid);
    
    
        return view;
    
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        closeDialog = (ImageView) getDialog().findViewById(R.id.closeDialog);
        title = (TextView) getDialog().findViewById(R.id.dialogTitle);
        gridView = (GridView) getDialog().findViewById(R.id.sub_grid);
    
        title.setText(String.format("Choose %s", mTitle));
        closeDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
            }
        });
    
    
    }
    
    @Override
    public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
    
    
        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
        //  closeDialog = (ImageView) dialog.findViewById(R.id.closeDialog);
    
        return dialog;
    
    }
    
    
    
    public void getAdapter() {
    
            gridAdapter = new HomeSubGridViewAdapter(getContext(), R.layout.gridview_custom_layout, liststring);
            gridView.setAdapter(gridAdapter);
    
    }
    
    
    }
    

    This is the method for calling dialog from fragment

      fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
                        SubcategoryFragment postalFragment = SubcategoryFragment.newInstance(Integer.valueOf(item.getId()), item.getName());
                        postalFragment.show(fragmentManager, "SubcategoryFragment");
    

    Feel Free to ask if you feel any problem is that

    0 讨论(0)
  • 2021-02-05 11:03

    If you want the kotlin version use this:

    val newDialogFragment = NewDialogFragment()
    val transaction: FragmentTransaction = 
    requireActivity().supportFragmentManager.beginTransaction()
    newDialogFragment.show(transaction, "New_Dialog_Fragment")
    
    0 讨论(0)
  • 2021-02-05 11:06

    I came across the same problem of not being able to show another DialogFragment from within the positive and negative click listeners of the first DialogFragment. My solution was to immediately pop the first fragment, which allows the second DialogFragment to attach and display successfully.

    // Call this before adding the second dialog fragment activity.getSupportFragmentManager().popBackStackImmediate();

    0 讨论(0)
提交回复
热议问题