ecem ecem - 4 months ago 64x
Android Question

Android: Preventing going back to the previous activity

I want to do this on Android: basically when BACK button is pressed on the phone, I want to prevent a specific activity to return to its previous one.

Specifically, I have login and sign up screens, both start a new activity called

when successful login/signup occurs. Once HomeScreen is started, I want to prevent the users to be able to return login or sign up screens with pressing BACK key.

I tried using
but since the application has Facebook integration, when the 'Login with Facebook' is used, Facebook should return to initial login screen, therefore I should keep a history of these activities.

I thought of overriding behaviour of BACK button on
to directly finish application when the button is pressed and I used

public void onBackPressed() {

but that also does not work.

I am quite new in Android development, so any help would be very useful.

Thanks in advance.


My suggestion would be to finish the activity that you don't want the users to go back to. For instance, in your sign in activity, right after you call startActivity, call finish(). When the users hit the back button, they will not be able to go to the sign in activity because it has been killed off the stack.