LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 January 21 2021

VipModz
Member

Android Studio logout dialog box

I want to show a "Are you sure you want to logout ?" dialog when the user attempts to logout.

To logout with one click (redirect the user to the registration page)

MainActivity

private void sendUserToMainActivity() {
        Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
        startActivity(intent);
        finish(); 

RegistrationActivity

case R.id.navigation_logout:
                    FirebaseAuth.getInstance().signOut();
                    Intent logoutIntent = new Intent(MainActivity.this, RegistrationActivity.class);
                    startActivity(logoutIntent);
                    finish();
                    break; 

is there a way to do add a dialog box using a code similar to this ??

@Override
    public void onBackPressed() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Are you sure you want to logout ?");
        builder.setCancelable(true);

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });


        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    } 

Offline

#2 January 21 2021

anayman_k7
Member

Re: Android Studio logout dialog box

Hello, your code of the Alert dialog in onBackPressed() looks good but for sure do not call it from onBackPressed, as far as I understood you want to call it when the navigation_logout is clicked, so simply make a function for it, something like that

private function promptLogoutConfirmation() {
        //Use the context of current activity
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Are you sure you want to logout ?");
        builder.setCancelable(true);

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                FirebaseAuth.getInstance().signOut();
                Intent logoutIntent = new Intent(MainActivity.this, RegistrationActivity.class);
                logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);//flags to clear your history
                startActivity(logoutIntent);
                dialogInterface.dismiss();
                finish();
                //dont forget to clear any user related data in your preferences
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });


        AlertDialog alertDialog = builder.create();
        alertDialog.show();
}

And use this function from the menu item click like that

case R.id.navigation_logout:
                    promptLogoutConfirmation();
                    break; 

Offline

#3 January 22 2021

VipModz
Member

Re: Android Studio logout dialog box

Thank you @anayman_k7 it worked !!

if any future person wants to do this don't forget to add

 return null;
}

Have a nice day !!

Offline

Board footer