Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent back button after logout in android

Tags:

java

android

After logout the user is directed to login screen in android. Now, if user clicks on back button of phone it should stay on login screen itself.

How can I make it possible in android?
I have used following code in my application but it will close my application. It should stay on the login screen only

Intent objsignOut = new Intent(getBaseContext(),Hello.class);
objsignOut.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objsignOut);

Please guide me the correct way.

like image 533
Smily Avatar asked Dec 08 '22 19:12

Smily


2 Answers

override the onBackPressed in your login activity, to do nothing..

public void onBackPressed() {
    //do nothing
}
like image 151
Nermeen Avatar answered Dec 11 '22 07:12

Nermeen


It seems to me that there are simpler and cleaner solutions than overriding onBackPressed method, as mentioned here and here.

You can provide flags when launching a new activity (on login or logout) to simply clear the "back-stack" rather than override the behavior for the back-button:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

This is a safer solution that can also be used after you log-in and not just after you log-out.

like image 29
Dean Gurvitz Avatar answered Dec 11 '22 07:12

Dean Gurvitz