views:

380

answers:

3

Hi everybody,

I'm planning to implement an Android application that requires a login screen.

If the user opens the activity something like this should happen:

  1. If user is logged in, goto 3
  2. If user is not logged in open the login screen and perfom login
  3. Show my application content

So, what's the "correct" way of implementing a login?

  1. Implement a StartActivity that perfoms the check if the user is logged in, implement a LoginActivity that implements the logging and an ApplicationActivity that actually implements the application logics?
  2. 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?

+1  A: 

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 StartActivity and LoginActivity into a single one.

kgiannakakis
A: 

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.

BrennaSoft
+1  A: 

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.

primalpop