简体   繁体   中英

Passing data from Fragment to DialogFragment

I'm trying to show a dialog when I get a Volley error and tell the user to retry again, so I choose a dialogFragment to be able to customize it.

I'm handling the error as follow in my Fragment class:

if (vError instanceof TimeoutError || vError instanceof NoConnectionError) {
                    Toast.makeText(getContext(),
                            Objects.requireNonNull(getActivity()).getString(R.string.error_network_timeout),
                            Toast.LENGTH_LONG).show();
                } else if (vError instanceof AuthFailureError) {
                    Toast.makeText(getContext(),
                            Objects.requireNonNull(getContext()).getString(R.string.error_network_auth_error),
                            Toast.LENGTH_LONG).show();
                } else if (vError instanceof ServerError) {
                    Toast.makeText(getContext(),
                            Objects.requireNonNull(getContext()).getString(R.string.error_network_server_error),
                            Toast.LENGTH_LONG).show();
                } else if (vError instanceof NetworkError) {
                    Toast.makeText(getContext(),
                            Objects.requireNonNull(getContext()).getString(R.string.error_network_network_error),
                            Toast.LENGTH_LONG).show();
                } else if (vError instanceof ParseError) {
                    Toast.makeText(getContext(),
                            Objects.requireNonNull(getContext()).getString(R.string.error_network_parse_faillure),
                            Toast.LENGTH_LONG).show();
                }

Actually I can only show Toast message per error type.

With the following, I'm trying to pass the message as an argument but doesn't seem to work.

Bundle args = new Bundle();
args.putString("vErr", "vErr");
DialogFragment errFragment = new NetworkErrorDialogFragment();
errFragment.setArguments(args);
errFragment.show(getFragmentManager(), "NetErrDialogFragment");

Edit: Retrieving the value:

In onCreateView of the dialogFragment:

errorTextView.setText(getArguments().getString("vErr"));

Are your receiving at

 onCreate(){
 getArguments
}

of NetworkErrorDialogFragment?

DialogFragment uses onCreateDialog() method for creating a dialog and its content view. so you need to override this method and set an appropriate content view to it:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog =new AppCompatDialog(getContext(), getTheme());
        dialog.setContentView(R.layout.some_content_view);
        // retreiving arguments here
        TextView errorTextView = dialog.findViewById(errorTextViewId);
        errorTextView.setText(getArguments().getString("vErr"));
        return dialog;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM