Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJoshua Silva
6,525 PointsAfter Logging In, I'd like to exit the app with the back button, not re-enter the Login screen. How do I do so?
I logged in with my made up user, however every time I immediately click the back button to exit the app it relocates me back to the login screen. I have the exact code written as Ben has it in his. Am I missing something?
3 Answers
Daniel Hartin
18,106 PointsHi Joshua
If you would never like to navigate back to the login activity simply use the line of code
finish();
after your call to startAcitivty(). This will close the activity and won't be available to navigate back to without restarting the app or specifically calling the activity later in your code. Another more complex and probably unnecessary way would be to implement the onBackPressed() method (don't forget to use the @override annotation) inside the new method and alter how you would like the back button press to be handled.
My personal preference for these things is to simply finish the login activity, if I do need to use this activity again I can simply call it up again using startActivity().
Hope this helps Daniel
Michael Partridge
7,129 PointsWhen starting the MainActivity after login you want to add intent flags:
//Once signed in go to MainActivity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);