Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent the back button in the login activity

I have an activity named MainActivity and another called LoginActivity. The main activity cannot be visible to logged out users. In the onCreate method of MainAcitivity I verifiy if the user is logged out and then I create the LoginActivity.

The problem is that if the user press the back button of the cellphone, the app go back to the MainActivity.

How I prevent this?

The code of the LoginAcitivity creation is this:

Intent i = new Intent(_context, LoginActivity.class);

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);         

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

_context.startActivity(i);
like image 578
Renan Cunha Avatar asked Jan 17 '26 14:01

Renan Cunha


2 Answers

You can call finish(); just after startActivity(). This will kill MainActivity so the back button will close the app.

like image 122
Jon Ander Peñalba Avatar answered Jan 20 '26 07:01

Jon Ander Peñalba


after: _context.startActivity(i);

put: finish();

like image 38
wernerH Avatar answered Jan 20 '26 07:01

wernerH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!