Event of the back button

Asked

Viewed 5,521 times

0

I have an application that has a login screen, it is the initial call of the app. When logging in, it is called Activitymain and from it the app follows its flow.

I would like to control the event of the back button of android, I would like to click the back button do not return to the Login Activity. The back button of the android itself, not the actionbar. I broke the code, but I don’t think it’s the best way to treat him

@Override
public void onBackPressed() {
    Intent intent = new Intent(MainActivity.this,MainActivity.class);
    startActivity(intent);
}
  • In Activitylogin, you have to give a Finish(); so that when the back is pressed it closes the main and goes back to android. Don’t forget to delete these changes in backpressed

  • 1

    It worked just fine Thank you very much.

1 answer

0


Henry,

You missed the Login Activity after opening Mainactivity.

Now, if you’ll allow me, it’s not good practice to change the back button. The back button aims to always go back to the previous screen, while the up button (which is in the actionbar) aims to go to the parent screen of the current screen, ie, to climb the hierarchy of screens. Take a look at this document.

  • Oops, thanks. Putting a Finish(); on the previous screen the backbutton does not come back. I have already withdrawn the implementation that is in the app question, thank you very much.

Browser other questions tagged

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