0

I use the EncryptedSharedPreferences to store value while login...i want to change text in navigation drawer of home activity

so when user is loggedin it will show logout else it will show login navigation drawer

Loginactivity:--------

 sharedPreferences = EncryptedSharedPreferences.create(
        "secret_shared_prefs",
        masterKeyAlias,
        baseContext,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    ) as EncryptedSharedPreferences

while login im doing this

  val editor = sharedPreferences.edit()
   editor.putString("EmailId", edtEmailId)
   editor.putString("password", edtPassword)
     editor.apply()

checking in Homeactivity:---------

        val menu: Menu = bind.navRightView.getMenu()

    val nav_connection: MenuItem = menu.findItem(R.id.nav_login)

   val sharedPreference =
            applicationContext.getSharedPreferences("secret_shared_prefs", Context.MODE_PRIVATE)
    val value: String = sharedPreference.getString("EmailId", null).toString()

    if(!sharedPreference.contains(value))
    {
        nav_connection.title = "Loginn"

    }
    else{
        nav_connection.title = "Logout"

    }

well output of this code is always having a title as loginn that means it detecting value as null need help for EncryptedSharedPreferences thanks

IRON MAN
  • 191
  • 3
  • 18
  • You shouldn't use actual `value` in the contains method, contains method expect key to be passed and not value as parameter. Check out reference: https://developer.android.com/reference/android/content/SharedPreferences#contains(java.lang.String) – Jeel Vankhede Dec 24 '20 at 13:52
  • You should try with `sharedPreference.contains('EmailId')`. – Jeel Vankhede Dec 24 '20 at 13:53
  • @JeelVankhede need help here https://stackoverflow.com/questions/65596957/onpaymentfailure-not-getting-called-in-razorpay – IRON MAN Jan 06 '21 at 14:06

1 Answers1

0

You are adding the values but not saving them to SharedPreferences.
You should be using commit() or apply().

Do this:

   val editor = sharedPreferences.edit()
   editor.putString("EmailId", edtEmailId)
   editor.putString("password", edtPassword)
   editor.apply() // Important

I also see that you are calling

val sharedPreference = applicationContext.getSharedPreferences("secret_shared_prefs", Context.MODE_PRIVATE)`

which gives you the normal SharedPreference, I guess.
You should be using:

val sharedPreferences = EncryptedSharedPreferences.create(
        "secret_shared_prefs",
        masterKeyAlias,
        baseContext,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    ) as EncryptedSharedPreferences


Then check if the email address is empty or not.

if(!sharedPreference.contains("EmailId") 
    nav_connection.title = "Loginn"
else 
    nav_connection.title = "Logout"
Darshan
  • 4,020
  • 2
  • 18
  • 49