0

I am having a project which uses Shared Preferences for Session Management. Everything is fine with the code but what really annoying is that the app is holding the session in Android Lollipop and above but unfortunately it is not holding the same for Android Kitkat and below. The session is lost whenever the app is closed and you have to lo in again. Following are the codes, I am using:

Session.java

package com.saptak.disputesession;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
/**
 * Created by Saptak Das on 27-02-2017.
 */

public class Session {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    Context context;

    public static String KEY_FNAME="namef";
    public static String KEY_LNAME="namel";
    public static String IS_LOGIN;

    public Session(Context context) {
        this.context = context;
        sharedPreferences=context.getSharedPreferences("userdetails",0);
        editor=sharedPreferences.edit();
    }

    public  void CreateLoginSession(String fname,String lname)
    {
        editor.putString(KEY_FNAME,fname);
        editor.putString(KEY_LNAME,lname);
        editor.putString(IS_LOGIN,"logged");
        editor.commit();
    }

    public HashMap<String,String> getdetails()
    {
        HashMap<String,String> details=new HashMap<>();
        details.put(KEY_FNAME,sharedPreferences.getString(KEY_FNAME,null));
        details.put(KEY_LNAME,sharedPreferences.getString(KEY_LNAME,null));
        return details;
    }

    public boolean loginstatus()
    {
        if(sharedPreferences.getString(IS_LOGIN,"unlogged").equals("logged"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void logoutac()
    {
        editor.clear();
        editor.commit();
    }
 }

Login.java

package com.saptak.disputesession;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Saptak Das on 27-02-2017.
 */

public class Login extends Activity {

    Button login;
    EditText first,last;

    Session session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        session=new Session(getApplicationContext());
        login=(Button)findViewById(R.id.log);
        first=(EditText)findViewById(R.id.fname);
        last=(EditText)findViewById(R.id.lname);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                session.CreateLoginSession(first.getText().toString(),last.getText().toString());
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                finish();
            }
        });
    }
}

MainActivity.java

package com.saptak.disputesession;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    Session session;
    Boolean flag;

    TextView tf,tl;

    Button logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        session=new Session(getApplicationContext());
        tf=(TextView)findViewById(R.id.xfname);
        tl=(TextView)findViewById(R.id.xlname);
        logout=(Button)findViewById(R.id.xlogout);
        flag=session.loginstatus();
        if(flag==false)
        {
            startActivity(new Intent(getApplicationContext(),Login.class));
            finish();
        }
        HashMap<String,String> details=session.getdetails();
        tf.setText(details.get(Session.KEY_FNAME));
        tl.setText(details.get(Session.KEY_LNAME));
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                session.logoutac();
                startActivity(new Intent(getApplicationContext(),Login.class));
                finish();
            }
        });
    }
}

This problem is getting on my nerves now as the app is perfectly coded, please help me out. Thanks in advance!

Update:

Please note, the problem is not about deleting the session, its completely opposite. The session is logging itself out everytime i am closing the app. and this problem is only in case of Android Kitkat and below, works fine for Android Lollipop and above

Saptak Das
  • 33
  • 8

1 Answers1

0

SharedPreferences allow you to save and retrieve data in the form of key,value pair.

Mistake

public static String IS_LOGIN="logged"; // Init here 

FYI

Session are useful when you want to store user data globally through out the application.

Rectify your method

public  void CreateLoginSession(String fname,String lname)
{
    editor.remove(KEY_FNAME); //Remove at first
    editor.remove(KEY_LNAME);
    editor.remove(IS_LOGIN);
    editor.putString(KEY_FNAME,fname);
    editor.putString(KEY_LNAME,lname);
    editor.putString(IS_LOGIN,"logged");
    editor.commit();
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198