Android Login – Best implementation
I’m planning to implement an Android application that requires a login screen.
If the user opens the activity something like this should happen:
- If user is logged in, goto 3
- If user is not logged in open the login screen and perfom login
- Show my application content
So, what’s the “correct” way of implementing a login?
- Implement a
StartActivitythat perfoms the check if the user is logged in, implement a
LoginActivitythat implements the logging and an
ApplicationActivitythat actually implements the application logics?
- Implement just one Activity and handle the login by using multiple views which I show according to the application state?
Are there any examples or tutorials for this scenario?
4 Solutions collect form web for “Android Login – Best implementation”
I don’t think that there is a “correct” way of doing this. Both solutions you describe seem fine to me, although I would merge
LoginActivity into a single one.
You can even just have a MainActivity that shows a Login Dialog. You could store the user’s login/password with SharedPreferences, and on app start check the preferences and auto-login the user or else show the login dialog. If you do this you should also add a logout functionality.
I had developed a microblogging application recently which had a login activity and which on logging in redirects user to his home page.
The way, I did it was to keep the login activity and Application Activity separate and if the user supplied credentials were right, the application activity was started by
startActivity(new Intent(LoginActivity.this, ApplicationActivity.class)
Later on I added a Checked Box and on ticking it, the user name and password were written to SharedPreferences and so the next time onwards user was straightaway directed to his home page.
- Login Activity : Taken you to the login screen if user is not logged in. Check SharedPreference for logged in user. If user is logged in, it will open up Application Activity.
- ApplicationActivity : The landing page of your application.