How to execute action after DialogFragment positive button clicked

前端 未结 4 435
梦谈多话
梦谈多话 2020-12-30 12:19

I created the following DialogFragment deriving it from the Android documentation:

public class PayBillDialogFragment extends DialogFragment{

    @Ov         


        
相关标签:
4条回答
  • 2020-12-30 12:34

    This is how I handle communication between fragment and dialog fragment

    Example fragment:

    public class MainFragment extends Fragment {
    
        private static final int REQ_CODE = 1;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.main_fragment, container, false);
            Button b = (Button) v.findViewById(R.id.button);
            b.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    MyDialog dialog = new MyDialog();
                    dialog.setTargetFragment(MainFragment.this, REQ_CODE);
                    dialog.show(getFragmentManager(), "dialog");
                }
            });
            return v;
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            Toast.makeText(getActivity(), "Result: " + resultCode,
                    Toast.LENGTH_SHORT).show();
            super.onActivityResult(requestCode, resultCode, data);
        }
    
    }
    

    Example DialogFragment:

    public class MyDialog extends DialogFragment {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage("My dialog message")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            notifyToTarget(Activity.RESULT_OK);
                        }
                    })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    notifyToTarget(Activity.RESULT_CANCELED);
                                }
                            });
            return builder.create();
        }
    
        private void notifyToTarget(int code) {
            Fragment targetFragment = getTargetFragment();
            if (targetFragment != null) {
                targetFragment.onActivityResult(getTargetRequestCode(), code, null);
            }
        }
    
    }
    

    This is the only method I got working also when changing orientation.

    0 讨论(0)
  • 2020-12-30 12:45

    To call dialog you can use this:

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    if (fm.findFragmentByTag("PayBillDialogFragment") == null) {
        PayBillDialogFragment payBillDialogFragment = new PayBillDialogFragment();
        payBillDialogFragment.show(fm, "PayBillDialogFragment");
    }
    

    In your dialogFragment,

    setPositiveButton("Paga", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           //Add your code here that should execute
           //when positive button is clicked
       }
    });
    
    0 讨论(0)
  • 2020-12-30 12:49

    The List Fragment would be using an Adapter to show elements. The adapter requires input in the form of some Collection. So, what you can do is, when the user presses say ok button on dialog fragment and you communicate that back to List Fragment, you can remove that particular element from the Collection and again set the Adapter of List Fragment with modified collection.

    0 讨论(0)
  • 2020-12-30 12:51

    You've got two options to call the ListFragment from PayBillDialogFragment.

    First one is recommended by the Android guidelines. All the communication goes through the hosting Activity. That's how you get the hosting Activity by calling ((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem() inside PayBillDialogFragment.setPositiveButton(onClick()). In HostingActivity.deleteItem() get the inflated PayBillDialogFragment and call some delete method in it.

    See http://developer.android.com/guide/components/fragments.html#EventCallbacks

    Second. You can just DialogFragment.setTargetFragment() when creating the DialogFragment object and then inside PayBillDialogFragment.setPositiveButton(onClick()) you can get PayBillDialogFragment by DialogFragment.getTargetFragment() and call the delete method there.

    See Callback to a Fragment from a DialogFragment

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