Skip to content Skip to sidebar Skip to footer

Where To Place Sign In Credentials Check - Android Dialog

I am trying to create a login credential screen when user clicks on a setting button. And if login credentials looks right then it should to settings screen. This is the official t

Solution 1:

TL;DR - Android Dialog Fragments by default close when a user clicks any button or list option in them. To prevent that, you need to override the onDismiss() method and continue with the default dismiss behaviour only if the user has entered the correct credentials.

According to the official Android documentation on dialog fragments,

When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.

The system also dismisses the dialog when the user touches an item in a dialog list, except when the list uses radio buttons or checkboxes. Otherwise, you can manually dismiss your dialog by calling dismiss() on your DialogFragment.

In case you need to perform certain actions when the dialog goes away, you can implement the onDismiss() method in your DialogFragment.

You can also cancel a dialog. This is a special event that indicates the user explicitly left the dialog without completing the task. This occurs if the user presses the Back button, touches the screen outside the dialog area, or if you explicitly call cancel() on the Dialog (such as in response to a "Cancel" button in the dialog).

As shown in the example above, you can respond to the cancel event by implementing onCancel() in your DialogFragment class.

So, you must check if your user's credentials are correct in the listener.onDialogPositiveClick() method. Then, you must update a boolean value, say hasUserEnteredCorrectCredentials to true. Then, you must override onDismiss() and allow it to dismiss the dialog only if hasUserEnteredCorrectCredentials is true, else keep the dialog open and show the user an error.

Remember, you will have to find a way to update the boolean hasUserEnteredCorrectCredentials from the place where you override the interface method.

So your code will become -

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;

publicclassSignInDialogFragmentextendsAppCompatDialogFragment {

    // Use this instance of the interface to deliver action eventsprivate SignInDialogListener listener;
    // This boolean checks whether the credentials are correctly enteredprivatebooleanhasUserEnteredCorrectCredentials=false;

    // Called when the dialog fragment is created.@NonNull@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
        // Initialise an alert dialog builder
        AlertDialog.Builderbuilder=newAlertDialog.Builder(getActivity());
        // Get the activity's default layout inflater to inflate the dialog fragmentLayoutInflaterinflater= requireActivity().getLayoutInflater();
        // Set the view to the inflated fragment
        builder.setView(inflater.inflate(R.layout.dialog_signin, null))
                // Set the positive button's text and onClickListener
                .setPositiveButton("Sign in", newDialogInterface.OnClickListener() {
                    @OverridepublicvoidonClick(DialogInterface dialog, int id) {
                        // Send the positive button event back to the host activity// TODO - Find a way to set hasUserEnteredCorrectCredentials to true if the credentials are correct.
                        listener.onDialogPositiveClick(SignInDialogFragment.this);
                    }
                })
                // Set the positive button's text and onClickListener
                .setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
                    @OverridepublicvoidonClick(DialogInterface dialog, int id) {
                        // Cancel the dialog
                        SignInDialogFragment.this.getDialog().cancel();
                    }
                });

        // Return the created dialog to the host activityreturn builder.create();
    }

    // Interface to manage clicks on the dialog's buttonspublicinterfaceSignInDialogListener {
        // ClickListener for the positive buttonvoidonDialogPositiveClick(SignInDialogFragment dialog);
    }

    // Override the Fragment.onAttach() method to instantiate the SignInDialogListener@OverridepublicvoidonAttach(@NonNull Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interfacetry {
            // Instantiate the NoticeDialogListener so we can send events to the host
            listener = (SignInDialogListener) context;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exceptionthrownewClassCastException(context.toString()
                    + " must implement SignInDialogListener");
        }
    }

    // Called when the user clicks a button, or when the user selects an option in a list dialog@OverridepublicvoidonDismiss(@NonNull DialogInterface dialog) {
        // Dismiss the dialog only if the user has entered correct credentialsif (hasUserEnteredCorrectCredentials) {
            super.onDismiss(dialog);
        }
    }
}

Hope this helps!

Post a Comment for "Where To Place Sign In Credentials Check - Android Dialog"