android 工作室中的共享首选项不起作用

Shared preferences in android studio not working

我正在制作一个登录页面,它使用共享首选项存储用户的登录数据,但是当我再次打开它时关闭应用程序后,登录 window 不应该出现,但它会出现即使在使用共享首选项之后,所以我每次打开它时都必须登录应用程序我试图找到错误但找不到,所以任何人都可以告诉我代码哪里出错了。

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()
    }


}

您的 属性 设置有错字

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

但是当你得到它时:

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