I am creating a project where I have a Registration screen, which is used for user to Register into the
Application. This Register screen should only be visible the first time, so the user can fill it and log in, but when user opens the application at the second time the application must show show Registration screen (only for registered user).
I don't understand how to do this.
I'm saving user credential in the code
private void setLoginData(String mobile, String pin, String emailId)
{
SharedPreferences pref = getActivity().getSharedPreferences("LoginInfo", 0);
Editor editor;
editor = pref.edit();
editor.putString("mobileNumebr", mobile);
editor.putString("pin", pin);
editor.putString("emailId", emailId);
editor.apply();
}
Your Code :
private void setLoginData(String mobile, String pin, String emailId)
{
SharedPreferences pref = getActivity().getSharedPreferences("LoginInfo", 0);
Editor editor;
editor = pref.edit();
editor.putString("mobileNumebr", mobile);
editor.putString("pin", pin);
editor.putString("emailId", emailId);
editor.putBoolean("login",true);
editor.apply();
}
Above I have added one boolean shared preference parameter. Then we'll
use that in Splash Screen for checking that user has already
registered or login or not.
on Splash Screen check this condition,
SharedPreferences prefs = getActivity().getSharedPreferences("LoginInfo", 0);
// then you use
boolean isLogin = prefs.getBoolean("login", false);
if(isLogin){
// Go to the Main Activity
}else{
// Go to the Registration Activity
}
Hope it will help you my friend!
https://stuff.mit.edu/afs/sipb/project/android/docs/training/id-auth/custom_auth.html The Account manager is a bit tricky It will collect credentials for accounts associated with your app only once and handle authentication transparently from then on if no exceptions occur. If lets say the wife changed credentials on her device and forgot to inform you, well Account manager will prompt you for the new password. The user can even go to the phone settings/accounts and access things that way once you implement Account Manager. The above is strictly for the local device but chances are you want to actually use credentials stored on a local device to authenticate with a remote server. if this is the case check this out https://stuff.mit.edu/afs/sipb/project/android/docs/training/id-auth/custom_auth.html?q=authenticationactivity#q=authenticationactivity
Related
My requirement is to differentiate between first time and subsequent Google-sign in in my android application. For first time user google sign-in, I want to redirect the user to the profile screen, where he will fill additional information regarding his contact details. After his first google sign in, all the subsequent google sign-in, I want the user to be directed directly to the home screen, without filling out the Profile details once again. I use Firebase for my backend. I have followed a couple of stack overflow questions and providing the link for the same below.
Link 1:
Correct code location to check Firebase if a user has been created already?
Link 2:
How to differentiate between first time google Sign-In and successive google sign-In in an android application?
I was able to achieve my requirement with a simple bug, which I couldn't figure out what I am missing. First time Google sign-in directs to Profile screen and subsequent requests redirects to Home screen. But what I tried, it happens only until the user closes the app. If the user closes the application and opens again, the user is redirected to the profile screen, which shouldn't happen. Once the user signs in with Google and has registered with the Profile, all the subsequent requests should go to Home screen.
My Bug: When the user closes the app and reopens again, the user is redirected to the Profile screen on google sign in. Here is what I have tried
SignInActivity.java
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(SignInActivity.this);
if (firebaseAuth.getCurrentUser() != null) {
if (!sharedPreferences.contains(String.valueOf(Preferences.FIRST_LAUNCH))) {
Intent contactDetailsIntent = new Intent(SignInActivity.this, ContactDetailsActivity.class);
startActivity(contactDetailsIntent);
sharedPreferences.edit().putBoolean(String.valueOf(Preferences.FIRST_LAUNCH), true).commit();
} else {
Intent loginIntent = new Intent(SignInActivity.this,MainMenuActivity.class);
startActivity(loginIntent);
finish();
}
}
Here is my preferences class.
public class Preferences {
public static final boolean FIRST_LAUNCH = false;
public static final String NAME = "fullName";
public static final String EMAIL = "email";
public static final String USERID = "userId";
public static final String USER_TYPE = "userType";
}
I am unable to figure out what the issue is. Any help is appreciated
To check if the user is logged in on their device, try this Firebase 9.0.0- How to see if user is logged in with AuthStateListener
I have used it for email authentication, but since it is linked to the user's account on firebse, I don't think it will matter. Also, I suggest putting it in the onCreate method.
To check for the number of times the user has logged in on their device, use firebase database. Get the Unique ID of the user using:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserID = user.getUid();
I would then check the database to see if the user with that unique ID exists. If it doesn't then store the ID in the database, create a child "timesLoggedIn" or something like that, and give it the value 0. If it does exist, then increment "timesLoggedIn".
i'm making an app in android, it has more than one option in the menu, and the login information is needed to proceed in every option, so i want to make the user sign only once not each time he's choosing one of the options like the facebook application i have to sign in only once even when the application is running in the background it doesn't logout..
sry 4 the long explanation but i don't know even the keyword for searching about this issue..
can u please help me guys ?
Thank u
Once the user enter the login credential first time then use SharedPreferences and set the value to isLogged to 1.
Next time when user open the app then check this variable if this variable value is '1' then open the home activity.
SharedPreferences sharedPref = getSharedPreferences("data",MODE_PRIVATE);
int number = sharedPref.getInt("isLogged", 0);
if(number == 0) {
//Open the login activity and set this so that next it value is 1 then this conditin will be false.
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("isLogged",1);
prefEditor.commit();
} else {
//Open this Home activity
}
I am building an app that uses the facebook android sdk and sso abilities. Every time you launch my app it will briefly show a facebook window while it logs in via the existing facebook app through sso. Is there a way to hide this screen and make sure my activity stays on top while it's check the facebook credentials and session etc.
For the first is user in not logged-in it ask it popup the dialog for login. But once user logged in and you try to send any facebook request, it check the session, if it is valid then it will not shown dialog.
When you login first time store the expire and access token to your shared preferences and on second time pass/set these(expire and access token to the facebook object) and check is Valid session.
Here is brief code;
stored you access token and expires in below method
facebook.authorize : onComplete listener:
e.g.
Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
for next time by passing these value to facebook object and check its valid ?
facebook = new Facebook(MyConstant.Fb_APPID);
String access_token = preferences.getString("access_token",
null);
long expires = preferences.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (facebook.isSessionValid()){
// do your work here
}
hope useful to you....
I am working on an application which requires me to create a log in screen. The way I have planned to do this is have 2 tabs log in, sign up and what I wanted to know is I want to be able to have a user sign up and if they choose remember password next time they load the app it should go straight to the main menu. Although selecting sign out in the main menu should load up the tabs with the log in information when they start the app again.
The question I have is how do I implement the remember me button so next time it skips the log in and how do I implement the sign out so next time the app loads the log in screen.
Thank you in advance! (",)
Sri
Throw the remembered answer into the SharedPreferences and read it when your activity starts and process it accordingly.
I would probably use Internal Storage to store the username/password for the remember me button.
When the app loads, first check if the user/pass is already saved. If so then direct to the tabs, if not then direct to the log in screen.
First Log In :
SharedPreferences sSession = PreferenceManager.getDefaultSharedPreferences(context);
Editor ePrefrences = sSession.edit();
ePrefrences.putString("id", "user id");
ePrefrences.putString("password", "user password");
ePrefrences.putBoolean("successfullylogin", true);
ePrefrences.commit();
Second Log In :
SharedPreferences sSession = PreferenceManager.getDefaultSharedPreferences(this);
if (sSession .getBoolean("successfullylogin", false)) {
//get user name and password
sUser = sSession.getString("id", "");
sPassword = sSession.getString("password", "");
//start activity
}
else {
//prepare for normal login
}
I want to show a message after the first launch of the app telling about the new features, each time a user installs a new version.
How do I do that?
Thanks in advance
I used this. It was posted by another stackoverflow member I don't recall who. Sorry. Call this code from onCreate()
// Show changelog
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
PackageInfo pInfo;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
if (prefs.getLong("lastRunVersionCode", 0) < pInfo.versionCode) {
showDialog(DIALOG_CHANGELOG);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("lastRunVersionCode", pInfo.versionCode);
editor.commit();
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Error reading versionCode");
e.printStackTrace();
}
Have an activity for this, that checks if a preference has the value of the current version. If so proceed to the next activity. Otherwise störe the current version in the preference, show the what's new screen and then on button press procees to the next activity
That's simple.
On the first start show an AlertDialog and create a record in SharedPreferences. Whatever - flag or a string, or somewhat.
The next time your application is started just check that you have this flag in preferences. If there is no one, then this is a first start and it's time to show a dialog =)
You have to save somewhere a value which you can check on each startup whether the app was already started. SharePreferences would be a option. Or within a database.
if the user clears application data in the device settings then this all gets reset.