Search code examples
androidauthenticationkotlinsharedpreferences

Error when using shared preferences to login in kotlin


Im trying to use shared preference in order to skip the login activity. If the user click in the checkbox it means that the next time he uses de app he will go directly to 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()
    }

Solution

  • I will show a example flow how you can skip the login fragment when once the user is registered or get's past from login fragment without skipping completely.

    When user launches app first time and login fragment shows up, after entering credentials and when user clicks login button save a string preference value like this.

            with(
                requireContext().getSharedPreferences(
                    "preference_login_key", Context.MODE_PRIVATE
                ).edit()
            ) {
                putString(
                    "preference_login_status",
                    "user_logged_in"
                )
                commit()
            }
    

    Now at this point we save preference string value as user_logged_in inside preference_login_status with key preference_login_key and going to next fragment.

    At any point if user logs out from the app you have to change the value as user_not_available inside preference_login_status with key preference_login_key shown below.

            with(
                requireContext().getSharedPreferences(
                    "preference_login_key", Context.MODE_PRIVATE
                ).edit()
            ) {
                putString(
                    "preference_login_status",
                    "user_not_available"
                )
                commit()
            }
    

    If you want to check whether there is an existing user from preferences, you can do this way and then decide at which point or destination the user has to be navigated to.

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

    In above code, first get the preference we are referring(preference_login_key) to and read the string(preference_login_status) stored inside it. Now check or compare the both strings(user_logged_in and user_not_available). If the strings don't match, there is no user logged in, take the user back to the login screen.

    Perform this check operations in onCreate() of the fragment.