In mutableLiveData<Int> , i am pushing USER_...
# android
m
In mutableLiveData<Int> , i am pushing USER_NOT_LOGGED_IN (==4) value in the starting of app from sharedpref. Still even these lines are executed, (I tested it by debugging), I am getting the livedata value 1. What is I am missing here?
a
Did you assign value 1 after the initial assignment?
m
No. I have debugged in the whole project wherever I am assigning value to the liveData. I only stuck on assigning 4 before getting into observer. And unfortunately I get 1 in the observed value.
h
@Md Razon Hossain Show initialization code liveData and USER_NOT_LOGGED_IN
image.png
m
Thank you everyone. I found the problem, it wasn't actually in livedata, it's the source from where I am setting data which is SharedPreference. While getting default value from sharedpreference, no matter what default value I set, I am only getting 1 as default value. I am attaching the sharedpref and MyApplication classess. Sharedpref class:
Copy code
class SharedPref() {

    companion object {


        private fun getSharedPreference(): SharedPreferences {
            return MyApplication.applicationContext.getSharedPreferences(
                BuildConfig.APPLICATION_ID,
                Context.MODE_PRIVATE
            )
        }

        private fun getSharedPreferenceEditor(): SharedPreferences.Editor {
            return getSharedPreference().edit()
        }

        fun setUserType(type: Int) {
            getSharedPreferenceEditor().putInt(USER_TYPE_PREF, type).apply()
        }


	
        fun getUserType(): Int {
            return getSharedPreference().getInt(USER_TYPE_PREF, 4)
        }

    }

}
MyApp Class:
Copy code
class MyApplication : MultiDexApplication() {

    init {
        INSTANCE = this
    }

    companion object {
        private lateinit var INSTANCE: MyApplication

        val applicationContext: Context
            get() {
                return INSTANCE.applicationContext
            }
    }
}
What am I missing here?
@Aslam Hossin vai please have a look.
h
You overwrite the value (_USER_TYPE_PREF_) elsewhere in your code.
Add log into
Copy code
fun setUserType(type: Int) {
            println("newValue $type")
            ...
}