Android Build a Weather App Concurrency and Error Handling What To Do When the Network is Down

bot .net
bot .net
10,072 Points

How to i add another Fragment for the no newtork connection?

Hi, question on the title, when i try to make another onCreateDialog method in my AlertDialogFrgament.java class i dont find the option, like i can only make 1 dialog.

any help?

Lauren Moineau Tonnie Fanadez Ben Jakuben

Ryan Dsouza
Ryan Dsouza
9,003 Points

It's SImple. You have to make a few modifications

In AlertDialogFragment

public class AlertDialogFragment extends DialogFragment {

    private String errorTitle;
    private String errorMessage;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Context context = getActivity();

//      Get activity gives us the context through which this class was called
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle(errorTitle)
                .setMessage(errorMessage)
                .setPositiveButton(getString(R.string.error_ok),null);

        return builder.create();
    }

    public void setErrorTitle(String errorTitle) {
        this.errorTitle = errorTitle;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}

In the MainActivity class

 AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.setErrorTitle("Opps! Sorry");
        dialog.setErrorMessage("There was an error. Please try again");
        dialog.show(getSupportFragmentManager(), "error_dialog");

This way, you can add a custom title message and text for each dialog box.