18

I am creating a project where I have a login screen, which is used for user to login into the
Application. This login 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 main.activity. How to use Shared preference.

I don't understand how to do this.

Kara
  • 6,115
  • 16
  • 50
  • 57

5 Answers5

38

To achieve this with SharedPreferences you might do something like this:

Insert the following in any Class you see more fit. Let's suppose you insert this in class Example.

//Give your SharedPreferences file a name and save it to a static variable
public static final String PREFS_NAME = "MyPrefsFile";

Now, in the method that evaluates if the user successfully logs in, do the following. Notice the Example class, you must change this to match your code.

//User has successfully logged in, save this information
// We need an Editor object to make preference changes.
SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();

//Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);

// Commit the edits!
editor.commit();

Finally, when your application starts you can now evaluate if the user has already logged in or not. Still notice the Example class that you must change.

SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0);
//Get "hasLoggedIn" value. If the value doesn't exist yet false is returned
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);

if(hasLoggedIn)
{
    //Go directly to main activity.
}

Hope this helps

EDIT: To prevent the user from using the back button to go back to the Login activity you have to finish() the activity after starting a new one.

Following code taken from Forwarding.java | Android developers

// Here we start the next activity, and then call finish()
// so that our own will stop running and be removed from the
// history stack
Intent intent = new Intent();
intent.setClass(Example.this, ForwardTarget.class);
startActivity(intent);
Example.this.finish();

So, what you have to do in your code is to call the finish() function on the Login activity, after calling startActivity().

See also: Removing an activity from the history stack

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
1

You should add another empty activity (with no UI) that loads before anything else.

Put the logic described by @Telmo Marques in this empty activity, which is responsible to direct the user either to the LoginScreen.Activity or to Main.Activity

see here, answer by @tozka How to Skip the first activity under a condition

Community
  • 1
  • 1
kouretinho
  • 2,190
  • 1
  • 23
  • 37
1

Using token is also a good method to know the login status.In Oauth token based login when the user login to the application they will get back a access token on successful login and is saved in account manager in secured way.And there after when ever the user open the application first check for the availability of token and if available redirect to the main page else to the login activity.

KJEjava48
  • 1,967
  • 7
  • 40
  • 69
1

Use SharedPreferences. For example, save some value, and read it on your login Activity.

In our project we saving token and user id. So, if user is already logged in, we skip authorization Activity.

P.S. If your login Activity is the first one in your app, then don't forget to finish it, before starting another Activity, to prevent pressing "Back" key in other activities.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
1

Use SharedPreferences. For example, have a boolean variable , and read it on your application launches. In your case when user launches the app first time the variable in shared preference will be false, so launch login screen and after successfull login make that boolean variable to true in shared preference, so that when user comes secnd time the value in the shared preference will be true. so skip the login screen and launch your main activity.

To store boolean in SharedPreference use below code::

public static void saveBooleanInSP(Context _context, boolean value){
SharedPreferences preferences = _context.getSharedPreferences("PROJECTNAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("ISLOGGEDIN", value);
editor.commit();
}//savePWDInSP()

To getValue from SharedPreference use below code::

public static boolean getBooleanFromSP(Context _context) {
// TODO Auto-generated method stub
SharedPreferences preferences = _context.getSharedPreferences("PROJECTNAME", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean("ISLOGGEDIN", false);
}//getPWDFromSP()
Ishu
  • 5,357
  • 4
  • 16
  • 17