How to make the back button close the activity without going back to the previous one (finish the app)? [closed]

-4

Activity login is as MAIN and is used to log in to fireBase. If the user is already logged in he goes directly to the activity of the main menu.

I wanted the activity on the main menu, when the user clicked the back button, did not return to the activity login, but rather that the application was terminated.

    
asked by anonymous 11.07.2017 / 16:36

2 answers

2

In the Activity login, just after the line that has startActivity() put finish(); . This will close the Activity login making clicking on the back does not return to it and the application is closed.

Anything like this:

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.

    
12.07.2017 / 19:53
0

Have you tried clearing the back stack of Activities?

Do not try to make the transition from the login screen to the other screen , add the flag: intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);

>

I hope it works!

    
12.07.2017 / 19:38