Reputation: 4110
I'll preface this with I'm really new to working on Android.
So I have to work on an existing app and create a screen that prompts the user for a username if one isn't entered then it won't launch the currently existing main activity (i.e. it won't fully launch the app. Instead it will just sit on that screen until a username is entered.)
I suppose that it's tantamount to a web app login page in that it will not let a user past that page if the user is not authenticated and authorized. However, there's no authentication nor authorization in this app. Instead, on first run a user must simply register with a username and that username gets saved to the database and, for any subsequent runs, the app's main activity will simply start because a user has already registered to that phone. In fact, the app will not prompt the user for their name again unless the app gets deleted with all its data and reinstalled again.
This implicitly means that I have to save the user's name in the database or some other kind of storage.
So I was wondering what would be the best way of doing this in an app with an existing main activity?
Should I try to accomplish this on that existing main activity or create a new activity to display this prompt screen and, in effect, block the main activity from running until the user enters their name?
Any tutorials or links would be helpful and thank you in advance for any help.
Upvotes: 0
Views: 380
Reputation: 3305
Maybe the same as raju's answer - I'm doing it this way:
Main Activity oncreate:
if (!prefs.contains("username")) {
startActivity(new Intent(this, Login.class));
finish();
}
Login class when login finished and "username" saved restart Main Activity:
finish();
startActivity(new Intent(getApplicationContext(), Main.class));
Very simple but performs nicely.
Upvotes: 0
Reputation: 8079
Create a login activity for user name and password.. If the user is new user.. registering for first time... then get the entered values and store them using shared preferences and open the activity..like this..
SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
pref.edit().putString("USERNAME", "PASSWORD").commit();<--Here use real username and password..
pref.edit().clear().commit();
Intent i=new Intent(this,youractivity.class);
startActivity(i);
else
let the user enter username and password fields.. and then check for them in shared preferences like this..
SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
String pswrd= pref.getString("Username", "somevalue");<--use entered username
if pswrd matches the entered password.. start the main activity..
if(pswrd==entered password){
Intent i=new Intent(this,youractivity.class);
startActivity(i);
}
hope it is clear..
Upvotes: 2
Reputation: 2854
Have your main activity (home intent) be a dialog activity that asks for credentials (username, etc.). Then when the user presses submit (or whatever you want to call your button), you start your app's "main" activity, i.e. the first activity after the login screen.
When launching the login screen, check to see if the user is already logged in. If they are, immediately start the "main" activity and bypass the login.
Upvotes: 3