How do I get the back button to close Activity without going back to the previous one (finish the app)?

Asked

Viewed 1,275 times

-4

Activity login is MAIN and is used to log in to firebase.
If the user is already logged in goes straight to Activity from the main menu.

I wanted that, in the Activity of the main menu, when the user clicked the back button did not return to the Activity login and yes that the application ended.

  • Your question is very bad, and there are several examples out there, it is better to do a survey before asking.

  • I can also suggest you to change the logic, like, Open Activity(Splashscreen)-> Checklogin (check if you are logged in)-> Main Menu, if not-> Login activity , and a reading on activity lifecycle.

2 answers

2

In Activity login, right after the line you have startActivity() place finish();. This will close the Activity login by making sure that clicking the back does not return to it and the application is closed.

Anything like that:

Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();

The same can be achieved by Intent flags:

Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

However, since there is only one Activity in the Stack, it is best to use the first method.

0

Have you ever tried to clear the back stack of Activities?

On the Internet that you do transition from login screen to other screen, add the flag: Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

I hope it works!

Browser other questions tagged

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