Download Code

Shared Preferences User Session Android

Storing user credentials is an important factor in an application.This mechanism helps user from login again and again.In this tutorial you will learn how to maintain user session in android application using Preferences.You will use Shared Preferences to maintain user session.

Preferences

Basic Preference UI building block.This class provides the View to be displayed in the activity.Associates with a SharedPreferences to store/retrieve the preference data.

Shared Preferences

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int).

User Session

Presence of a particular user of using app or website.

Create new android application project.Go to activity_main.xml and add EditText,Button.

Shared Preferences,preferences,sharedpreferences,preference in android,sharedpreference in android, sharedpreference in android tutorial,how to read preference in android, how to read sharedpreference in android, sharedpreference android tutorial, session management in android, user session management android, user session management android tutorial, session management android tutorial, how to maintain user session android, how to maintain user session in android tutorial, how to maintain session android, login logout session android, login logout session android tutorial, how to keep login user in android, how to keep login user in android tutorial, keep user login android tutorial, keep user login android source code, how to add values in preferences, how to add values in preferences android,

Create new XML layout(home.xml) which display when you login.

Shared Preferences,preferences,sharedpreferences,preference in android,sharedpreference in android, sharedpreference in android tutorial,how to read preference in android, how to read sharedpreference in android, sharedpreference android tutorial, session management in android, user session management android, user session management android tutorial, session management android tutorial, how to maintain user session android, how to maintain user session in android tutorial, how to maintain session android, login logout session android, login logout session android tutorial, how to keep login user in android, how to keep login user in android tutorial, keep user login android tutorial, keep user login android source code, how to add values in preferences, how to add values in preferences android,

Next go to MainActivity.java.Do the following steps.

  • Create UI Component variables(EditText,Button).
  • Refer id’s of them.
  • Create Preferance Name variable.
  • Create variables for user credentials like email,pass etc.
  • Create a method which run first to check credentials of user.

MainActivity.java

package developer.codecusersession;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener {

	EditText email,password;
	Button signIn;
	public static final String userSessionPref = "userPrefs";
	public static final String User_Email = "userEmail";
	public static final String User_Password = "userPassword";
	SharedPreferences session;
	
	String SESSION_EMAIL,SESSION_PASS;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkCredentials();
        initialize();
        
        session = getSharedPreferences(userSessionPref, Context.MODE_PRIVATE);
    }
    public void initialize(){
    	email = (EditText) findViewById(R.id.userEmail);
    	password = (EditText) findViewById(R.id.userPass);
    	signIn = (Button) findViewById(R.id.signInBtn);
    	signIn.setOnClickListener(this);
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.signInBtn:
			SESSION_EMAIL = email.getText().toString();
			SESSION_PASS = password.getText().toString();
			SharedPreferences.Editor session_editor = session.edit();
			session_editor.putString("email", SESSION_EMAIL);
			session_editor.putString("pass", SESSION_PASS);
			session_editor.commit();
			Intent n = new Intent(getApplicationContext(),Home.class);
			startActivity(n);
			finish();
			break;
		}
	}
// method to check existing credentials
    public void checkCredentials(){
    	SharedPreferences preferences = getSharedPreferences(userSessionPref,Context.MODE_PRIVATE);
    	String email = preferences.getString("email", "");
    	String pass = preferences.getString("pass", "");
    	if(!email.equalsIgnoreCase("") && !pass.equalsIgnoreCase(""))
    	{
    		Intent n = new Intent(getApplicationContext(),Home.class);
			startActivity(n);
			finish();
    	}
    	
    }
	
}

Create new activity/class for home layout.In this activity one button is used to logout user.

Home.java

package developer.codecusersession;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Home extends Activity{

	Button signOut;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		signOut = (Button) findViewById(R.id.out);
		signOut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences session = getSharedPreferences(MainActivity.userSessionPref, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = session.edit();
				editor.clear();
				editor.commit();
				Intent m = new Intent(getApplicationContext(),MainActivity.class);
				startActivity(m);
				finish();
			}
		});
	}
}

At last don’t forget to add Home.java activity in Android Manifest.xml file.

Shared Preferences,preferences,sharedpreferences,preference in android,sharedpreference in android, sharedpreference in android tutorial,how to read preference in android, how to read sharedpreference in android, sharedpreference android tutorial, session management in android, user session management android, user session management android tutorial, session management android tutorial, how to maintain user session android, how to maintain user session in android tutorial, how to maintain session android, login logout session android, login logout session android tutorial, how to keep login user in android, how to keep login user in android tutorial, keep user login android tutorial, keep user login android source code, how to add values in preferences, how to add values in preferences android,

Run your Shared Preferences android application project.

Shared Preferences,preferences,sharedpreferences,preference in android,sharedpreference in android, sharedpreference in android tutorial,how to read preference in android, how to read sharedpreference in android, sharedpreference android tutorial, session management in android, user session management android, user session management android tutorial, session management android tutorial, how to maintain user session android, how to maintain user session in android tutorial, how to maintain session android, login logout session android, login logout session android tutorial, how to keep login user in android, how to keep login user in android tutorial, keep user login android tutorial, keep user login android source code, how to add values in preferences, how to add values in preferences android,

That’s it for this tutorial.

Thanks.