使用共享首选项登录 kotlin 时出错
Error when using shared preferences to login in kotlin
我正在尝试使用共享首选项来跳过登录 activity。如果用户点击复选框,则意味着他下次使用 de app 时将直接转到 de main activity.Here is the code
val checkbox = checkBoxLog
val sharedPref:SharedPreferences = this.getSharedPreferences("prefName", Context.MODE_PRIVATE)
val editor:SharedPreferences.Editor = sharedPref.edit()
if(checkbox.isChecked){
editor.putBoolean("Checkbox", true)
editor.apply()
Log.d("hola", "presente")
startActivity(Intent(this, MainActivity::class.java))
finish()
}
我将展示一个示例流程,说明如何在用户注册后跳过登录片段或从登录片段获取过去而不完全跳过。
当用户第一次启动应用程序并显示登录片段时,在输入凭据后以及当用户单击登录按钮时,保存这样的字符串首选项值。
with(
requireContext().getSharedPreferences(
"preference_login_key", Context.MODE_PRIVATE
).edit()
) {
putString(
"preference_login_status",
"user_logged_in"
)
commit()
}
此时我们将首选项字符串值保存为 user_logged_in
在 preference_login_status
中,使用键 preference_login_key
并转到下一个片段。
在任何时候,如果用户从应用程序注销,您必须在 preference_login_status
中使用键 preference_login_key
将值更改为 user_not_available
,如下所示。
with(
requireContext().getSharedPreferences(
"preference_login_key", Context.MODE_PRIVATE
).edit()
) {
putString(
"preference_login_status",
"user_not_available"
)
commit()
}
如果您想从首选项中检查是否存在现有用户,您可以这样做,然后决定用户必须导航到的位置或目的地。
val sharedPref = requireContext().getSharedPreferences(
"preference_login_key", Context.MODE_PRIVATE
)
with(
sharedPref.getString(
"preference_login_status",
"user_not_available"
)
) {
if (!this.equals("user_logged_in")) {
// User is not logged-in, so take him back to login fragment
}
}
在上面的代码中,首先获取我们引用的首选项(preference_login_key
)并读取其中存储的字符串(preference_login_status
)。现在检查或比较两个字符串(user_logged_in
和 user_not_available
)。如果字符串不匹配,则没有用户登录,将用户带回登录屏幕。
在片段的 onCreate()
中执行此检查操作。
我正在尝试使用共享首选项来跳过登录 activity。如果用户点击复选框,则意味着他下次使用 de app 时将直接转到 de main activity.Here is the code
val checkbox = checkBoxLog
val sharedPref:SharedPreferences = this.getSharedPreferences("prefName", Context.MODE_PRIVATE)
val editor:SharedPreferences.Editor = sharedPref.edit()
if(checkbox.isChecked){
editor.putBoolean("Checkbox", true)
editor.apply()
Log.d("hola", "presente")
startActivity(Intent(this, MainActivity::class.java))
finish()
}
我将展示一个示例流程,说明如何在用户注册后跳过登录片段或从登录片段获取过去而不完全跳过。
当用户第一次启动应用程序并显示登录片段时,在输入凭据后以及当用户单击登录按钮时,保存这样的字符串首选项值。
with(
requireContext().getSharedPreferences(
"preference_login_key", Context.MODE_PRIVATE
).edit()
) {
putString(
"preference_login_status",
"user_logged_in"
)
commit()
}
此时我们将首选项字符串值保存为 user_logged_in
在 preference_login_status
中,使用键 preference_login_key
并转到下一个片段。
在任何时候,如果用户从应用程序注销,您必须在 preference_login_status
中使用键 preference_login_key
将值更改为 user_not_available
,如下所示。
with(
requireContext().getSharedPreferences(
"preference_login_key", Context.MODE_PRIVATE
).edit()
) {
putString(
"preference_login_status",
"user_not_available"
)
commit()
}
如果您想从首选项中检查是否存在现有用户,您可以这样做,然后决定用户必须导航到的位置或目的地。
val sharedPref = requireContext().getSharedPreferences(
"preference_login_key", Context.MODE_PRIVATE
)
with(
sharedPref.getString(
"preference_login_status",
"user_not_available"
)
) {
if (!this.equals("user_logged_in")) {
// User is not logged-in, so take him back to login fragment
}
}
在上面的代码中,首先获取我们引用的首选项(preference_login_key
)并读取其中存储的字符串(preference_login_status
)。现在检查或比较两个字符串(user_logged_in
和 user_not_available
)。如果字符串不匹配,则没有用户登录,将用户带回登录屏幕。
在片段的 onCreate()
中执行此检查操作。