How to execute action after DialogFragment positive button clicked

一笑奈何 提交于 2019-11-30 07:23:39

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.

riwnodennyk

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

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
   }
});

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!