Search code examples
androidandroid-studiokotlinsharedpreferences

Shared preferences in android studio not working


I was making a login page which stores the login data of the user using shared preferences, but after closing the app when I open it again the log-in window should not come, but it is coming even after using shared preferences, so I have to log-in app every time I open it I tried to find error but couldn't, so anyone can please tell me where the code is going wrong.

package com.kartik.activitylifecycle

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

class LoginActivity : AppCompatActivity(){

     lateinit var etMobileNumber: EditText
     lateinit var etPassword: EditText
     lateinit var btnLogin: Button
     lateinit var txtForgotPassword: TextView
     lateinit var txtRegister: TextView
      val validMobileNumber="9999"
    val validPassword= arrayOf("tony","steve","thanos","bruce")

    lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        sharedPreferences = getSharedPreferences(getString(R.string.preference_file_name), Context.MODE_PRIVATE)

        val isLoggedIn = sharedPreferences.getBoolean("isLoggedIn",false)
        if(isLoggedIn){
            val intent = Intent(this@LoginActivity,AvengersActivity::class.java)
            startActivity(intent)
        } else{
            setContentView(R.layout.activity_login)
        }


        title = "Log In"
        etMobileNumber=findViewById(R.id.etMobileNumber)
        etPassword=findViewById(R.id.etPassword)
        btnLogin = findViewById(R.id.btnLogin)
        txtForgotPassword=findViewById(R.id.txtForgotPassword)
        txtRegister=findViewById(R.id.txtRegisterYourself)


        btnLogin.setOnClickListener {
            val mobileNumber = etMobileNumber.text.toString()
            val password= etPassword.text.toString()
            var nameofAvenger="Avenger"
            val intent = Intent(this@LoginActivity,AvengersActivity::class.java)

            if((mobileNumber == validMobileNumber)) {
                if (password == validPassword[0]) {

                    savePreferences()

                    nameofAvenger = "IronMan"
                    intent.putExtra("Name", nameofAvenger)
                    startActivity(intent)
                } else if (password == validPassword[1]) {

                    savePreferences()

                    nameofAvenger = "Captain America"
                    intent.putExtra("Name", nameofAvenger)
                    startActivity(intent)
                }  else if (password == validPassword[2]) {

                    savePreferences()

                    nameofAvenger = "The Hulk"
                    intent.putExtra("Name", nameofAvenger)
                    startActivity(intent)
                }  else if (password == validPassword[3]) {

                    savePreferences()

                    nameofAvenger = "The Avenger"
                    intent.putExtra("Name", nameofAvenger)
                    startActivity(intent)
                }



            }
            else{
                Toast.makeText(this@LoginActivity, "Wrong user id/password entered",Toast.LENGTH_LONG).show()
            }


        }
    }

    override fun onPause() {
        super.onPause()
        finish()
    }
      fun savePreferences(){
        sharedPreferences.edit().putBoolean("is LoggedIn",true).apply()
    }


}

Solution

  • You have a typo in your property setting

    sharedPreferences.edit().putBoolean("is LoggedIn",true).apply()
    

    But when you get it:

    val isLoggedIn = sharedPreferences.getBoolean("isLoggedIn",false)