Hamid Akhtar
Hamid Akhtar

Reputation: 11

Unable to get some SharedPreferences value after app restart

Inside the onCreate function I have this code:

toggle_biometric = switchView.findViewById(R.id.toggle_biometric)
toggle_biometric.isOn = pref.getPrefBoolean(this@HomeActivity, pref.biometric_enable)
toggle_biometric.setOnToggledListener { toggleableView, isOn ->
    pref.setPrefBoolean(applicationContext, pref.biometric_enable, isOn)
    biometricLogin = isOn
    Toast.makeText(this@HomeActivity, "Biometric Login: $biometricLogin", Toast.LENGTH_SHORT).show()
}

And here is my onNavigationItemSelected:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.nav_biometric -> {
            // This will trigger the LabeledSwitch toggle
            toggle_biometric.isOn = !toggle_biometric.isOn
            // Update preference and show toast
            val isChecked = toggle_biometric.isOn
            pref.setPrefBoolean(applicationContext, pref.biometric_enable, isChecked)
            if(isChecked) {
                biometricLogin = isChecked
            }
            else {
                biometricLogin = isChecked
            }
        }  
    }

    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}

And this is my onResume:

override fun onResume() {
    super.onResume()

    toggle_biometric.isOn = pref.getPrefBoolean(applicationContext , pref.biometric_enable)
}

Other sharedPreference value like token is getable your text le but Switch state unable to save. Changing of state can see correctly through Toast message but can not able to save .Every time getting default value false Please help.

Upvotes: 1

Views: 70

Answers (0)

Related Questions