OTP Verification/Phone Verification in Android

Jyotishgher Astrology
By -
0

 OTP Verification/Phone Verification in Android

To skip the OTP login page once the OTP has been verified at least once, you can use SharedPreferences to store a flag like otp_verified=true when verification succeeds. On future launches, check this flag in onCreate() and redirect the user to the Dashboard directly.

Zoom image will be displayed
OTP login page Android

Step-by-step changes:

1. Set flag when OTP is verified

In insertOtpToCmms() method, after successful login (whether success == 1 or success == 2), save a flag in SharedPreferences.

SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(“otp_verified”, true);
editor.apply();

For Example

So, your if (success == 1) and if (success == 2) blocks become:

if (success == 1) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("otp_verified", true);
editor.apply();

Intent intent = new Intent(MainActivity.this, entry_page_register.class);
intent.putExtra("MOBILE", mobile);
intent.putExtra("TYPE", "LOGIN");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else if (success == 2) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("otp_verified", true);
editor.apply();

Intent intent = new Intent(MainActivity.this, dashboard.class);
intent.putExtra("MOBILE", mobile);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

2. Check this flag in onCreate() before showing the login UI

Add the following at the very top of your onCreate():

settings = getSharedPreferences(MyPREFERENCES, 0);
boolean isOtpVerified = settings.getBoolean(“otp_verified”, false);

if (isOtpVerified) {
// Redirect to dashboard and finish this activity
Intent intent = new Intent(MainActivity.this, dashboard.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // Don’t allow returning to OTP screen
return;
}

Optional: To reset OTP (e.g. during logout)

When user logs out, you can clear this flag like this:

SharedPreferences.Editor editor = getSharedPreferences(MyPREFERENCES, 0).edit();
editor.remove(“otp_verified”); // or editor.clear() to wipe all
editor.apply();

Final result:

  • First login → OTP shown → Verified → Dashboard
  • Next time opening app → Goes directly to Dashboard
  • Only shows OTP screen if not verified yet or after logout.
Tags:

Post a Comment

0Comments

Post a Comment (0)