From the course: Android Development Essential Training: Manage Data with Kotlin
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Read default shared preferences
From the course: Android Development Essential Training: Manage Data with Kotlin
Read default shared preferences
My app now has a preferences screen, and I want to use one of its settings to change the title displayed in the tool bar. I'll show how to read preferences into memory from within my view model class. I'll go to my shared view model class, and I'll create a new mutable live data property, and I'll name it 'activity title'. This mutable live data object with wrap a string. Next I'll create a function that I'll name 'update activity title', and within that function, I'll get the value from the preferences like this. I'll create a variable they'll call 'signature', and I'll use this expression. I'll start with preference manager, and this is from Android x dot preference. Then I'll call the function 'getDefualtSharedPreferences'. I want to pass in my app as my context, but to get to that, I need to make it last a little bit longer. I'll just add the keyword 'val' up here, and now the app reference will be available for the lifetime of the view model, and I can pass it in. Now I have the…