How to keep android applications always be logged in state?

You may add the following for logout operation in your SaveSharedPreference.java :

public static void clearUserName(Context ctx) 
{
    Editor editor = getSharedPreferences(ctx).edit();
    editor.clear(); //clear all stored data
    editor.commit();
}

Always and whenever ... so this seems like an automated login approach. The application starts, the user doesn't have to login again once he has provided his authorization information. There are several ways to achieve this. The Facebook API e.g. provides mechanism for login without the use of a form element. So you would basically have to construct a view in which the user can provide his authorization information once, store them in SharedPreferences and login automatically the next time. Even if there is no such API - based login and you have to use a login - form provided, you sometimes could simulate the POST - request for logging in. This depends on the actual server - side implementation of that form. But remember: This is an security issue. So never store authorization credentials in clear text format. Don't rely on crypting it by a defined key in your application as this key maybe detected by reverse engineering your APK. Instead use many collective informations about the device and a key the user has to enter or is randomly generated and stored by the app.


I am surprised no answer mentions AccountManager. It is the official and correct way to find out if your user is really who he/she claimed to be. In order to use it, you have to get permission by adding this in your manifest file :

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Then you get an array of Accounts using this (in case you want user to use his/her Google account) :

AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.google");

Full reference here : AccountManager


Use Shared Preference for auto login functionality. When users log in to your application, store the login status into sharedPreference and clear sharedPreference when users log out.

Check every time when the user enters into the application if user status from shared Preference is true then no need to log in again otherwise direct to the login page.

To achieve this first create a class, in this class you need to write all the function regarding the get and set value in the sharedpreference. Please look at this below Code.

public class SaveSharedPreference 
{
    static final String PREF_USER_NAME= "username";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    public static void setUserName(Context ctx, String userName) 
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(PREF_USER_NAME, userName);
        editor.commit();
    }

    public static String getUserName(Context ctx)
    {
        return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
    }
}

Now in the main activity (The "Activity" where users will be redirected when logged in) first check

if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
     // call Login Activity
}
else
{
     // Stay at the current activity.
}

In Login activity if user login successful then set UserName using setUserName() function.