George Hilliard
George Hilliard

Reputation: 15952

Making Retrofit request from DialogFragment

I have implemented a login DialogFragment in my app (API 19). I'm using Retrofit 1.7.1 for networking. The implementation is below:

public class SignInDialogFragment extends DialogFragment {
    private EditText mUsernameEditText;
    private EditText mPasswordEditText;

    public static SignInDialogFragment newInstance() {
        return new SignInDialogFragment();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Builder builder = new AlertDialog.Builder(getActivity())
                .setPositiveButton(R.string.action_sign_in, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String username = mUsernameEditText.getText().toString();
                        APIFactory.getAPI().register(username, new Callback<RegistrationResponseModel>() {
                            @Override
                            public void success(RegistrationResponseModel registrationResponse, Response response) {
                                Toast.makeText(getActivity(), "Totally worked!", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void failure(RetrofitError error) {
                                Toast.makeText(getActivity(), "Something failed!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                })
                .setNegativeButton(R.string.action_cancel, null);

        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_sign_in, null);
        builder.setView(view);

        mUsernameEditText = (EditText)view.findViewById(R.id.usernameEditText);

        return builder.create();
    }
}

As you might predict, this crashes upon success or failure when getActivity() hits a null reference. What is the best way to handle an asynchronous request such as this, including getting callbacks after the dialog has been dismissed?

I thought of a few options; are any of them viable?

I could simply hold the dialog open if there is a request in progress. I haven't implemented this because I'm not sure of the cleanest way to keep the dialog around (handling the Back button, etc).

I would really like to just cancel the Request if the dialog is closed, but I can't, because there is not currently a way to cancel Retrofit requests.

If there is an option that I'm not aware of, I'd like to hear about it.

Upvotes: 0

Views: 1515

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

You should move your action out of Dialog. Dialog is not Activity - once user did a choice, dialog should be dismissed and if there's any action to trigger it should be done as response to user action with dialog, but it should not be part of dialog. Therefore you should just react to user choice in your parent Fragment or Activity, not in Dialog.

Upvotes: 4

Related Questions