Android how can I check user logged in or logged out using Shared Preferences

3848 views java

Here is my for using just login into system without signup by using Shared Preferences. When I clicked Sign in button it will save input data into Preference. Now I need to use session for checking user logged in or not logged out. But I don't able to do this. Please help me. Advance Thanks ????

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


//login class
public class MainActivity extends AppCompatActivity {

    private EditText Email, Password;
    private Button btn_signin;
    private TextView errordia;
    private SharedPreferences pref;
    private static final String FILE_NAME = "UserInfo";
    private String PARAM_ONE = "Email";
    private String PARAM_TWO = "Password";
    private String PARAM_THREE = "userLoggedInState";
    private boolean isUserLoggedIn;

    protected void onCreate(Bundle savedInstanceState) {

        Email = (EditText)findViewById(;
        Password = (EditText)findViewById(;
        btn_signin = (Button)findViewById(;
        errordia = (TextView)findViewById(;
        btn_signin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

    private void SaveData(){
        String mEmail = Email.getText().toString();
        String mPass = Password.getText().toString();

        if (mEmail.equals("faruk") && mPass.equals("1234")){
            SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(PARAM_ONE, mEmail);
            editor.putString(PARAM_TWO, mPass);
            editor.putBoolean(PARAM_THREE, true);
            Toast.makeText(getApplicationContext(), "Data is saved successfully", Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(getApplicationContext(), "Provide Valid Information",Toast.LENGTH_LONG).show();


    private void LoadData(){
        SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
        String saved_email = pref.getString(PARAM_ONE, null);
        String saved_password = pref.getString(PARAM_TWO, null);
        errordia.setText("Email : " + saved_email + " Password: " + saved_password);

    private void Login(){
        SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
        if (pref.contains(PARAM_ONE) && pref.contains(PARAM_TWO)){
            Intent intent = new Intent(MainActivity.this, MenuPage.class);

answered question

1 Answer


Add thge logout button on your app.
When user click on logout clear the SharedPreferences login data.

SharedPreferences sharedPreferences = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();

posted this

Have an answer?


Please login first before posting an answer.