Android Development #6: Login Framework Using SharedPreference

First posted on 01/04/2014

Source Code


  1. Create MainActivity that extends FragmentActivity
  2. Host two Fragments under MainActivity
    • MainFragment extends Fragment
    • LoginFragment extends Fragment
  3. Create a SessionManager class that will handle SharedPreference stuff
    public class SessionManager {
        SharedPreferences pref;
        SharedPreferences.Editor editor;
        Context _context;
        int PRIVATE_MODE = 0;
        private static final String PREF_NAME = "credentials"; 
        private static final String IS_LOGIN = "IsLoggedIn";
        public static final String KEY_NAME = "username";
        public static final String KEY_PASSWORD = "password";
        public SessionManager(Context context) {
            this._context = context;
            pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
            editor = pref.edit();
         * Create login session
        public void createLoginSession(String name,
                                       String password) {
            editor.putBoolean(IS_LOGIN, true);
            editor.putString(KEY_NAME, name);
            editor.putString(KEY_PASSWORD, password);
         * Check login method wil check user login status
         * If false it will redirect user to login page
         * Else won't do anything
        public void checkLogin() {
            // Check login status
            if (!this.isLoggedIn()) {
    /*            // user is not logged in redirect him to Login Activity
                Intent i = new Intent(_context, LoginActivity.class);
                // Closing all the Activities
                // Add new Flag to start new Activity
                // Starting Login Activity
         * Get stored session data
        public HashMap<String, String> getUserDetails() {
            HashMap<String, String> user = new HashMap<String, String>();
            user.put(KEY_NAME, pref.getString(KEY_NAME, null));
            user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
            // return user
            return user;
         * Clear session details
        public void logoutUser() {
            // Clearing all data from Shared Preferences
         * Quick check for login
         * *
        // Get Login State
        public boolean isLoggedIn() {
            return pref.getBoolean(IS_LOGIN, false);

XML Layouts

  1. In MainActivity, set up an item action for Option Menu
  2. In MainFragment, create a fragment_main.xml
    • Create two TextViews
    • Create two Button
  3. In LoginFragment, create a fragment_login.xml
    • Create two EditText
    • Create a Button

App Flow

  1. In LoginFragment, users enters Username and Password and press a Button
  2. Username and Password is stored in a SharedPreference handled by SessionManager class
    • If fields are empty, display a Toast
  3. User will be brought to MainFragment after logged in
    • MainFragment will only show logged in components
  4. User press Logout in action item. SessionManager clears SharedPreference data.
  5. User will be brought back to MainFragment after logged out
    • MainFragment will only show non-logged in components

To see this file in local storage, navigate to /data/data/aiman.demo.logintask/shared_prefs/credentials.xml. Make sure your phone is rooted.


  • During data storing in SharedPreference, call web service for value comparison with server side database
  • Store only insensitive value in SharedPreference. Keep sensitive data as 'dynamic' as possible.
Show Comments

Get the latest posts delivered right to your inbox.