Do not allow onBackPressed to run

Asked

Viewed 352 times

0

In my application, when the user logs in if he or she presses the onBackPressed() from his smartphone the app should not return to the previous screen. When he logs in and I change Activity I set in Intent the following code:

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

In some cell phones the following code works.. In others it does not. What to do?

1 answer

2

To prevent the onBackPressed() do something about anything Activity just overwrite it on Activity in question:

@Override
public void onBackPressed() {
    // Não faz nada
}

But in your case I think the ideal would be to finish the Activity login calling finish() shortly after the startActivity() initiating the second Activity:

startActivity(new Intent(this, SegundaActivity.class));
finish();

Thus the login Activity exits the activity stack and becomes the second Activity at the top.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.