Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moster 07 persisting data in shared preferences #7

Merged
merged 4 commits into from
May 1, 2020

Conversation

AnelCC
Copy link
Owner

@AnelCC AnelCC commented May 1, 2020

Moster 07 persisting data in shared preferences

In Android, a shared preference is a value that's stored in an unencrypted XML file. It's stored in the app's internal storage area, so it isn't visible to the user. You don't have to read or write the XML file directly, that's all done by the Android SDK for you.

1. Manage shared preferences with code

First, I need a way of getting access to a preferences XML file, this is represented in code as an instance of the shared preferences class. So, I'll create a new function that I'll call preferences and it'll receive an instance of the context.
I need some functions to set and get a particular preference. Each preference has a key, a string because each preference is a key value pair. So, up here above the class I'll create a new variable and I'll name it ITEM_TYPE_KEY

 fun setItemType(context: Context, type: String) {
            preferences(context).edit()
                .putString(ITEM_TYPE_KEY, type)
                .apply()
        }
fun getItemType(context: Context): String =
            preferences(context).getString(ITEM_TYPE_KEY, "list")!!

2. Use preferences to manage display

I will modify the view and safe with configuration settings in the OptionsItemSelected
PreferencesHelper.setItemType(requireContext(), "list") or PreferencesHelper.setItemType(requireContext(), "grid")

tab

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_view_grid -> {
                PreferencesHelper.setItemType(requireContext(), "grid")
                recyclerView.layoutManager =
                    GridLayoutManager(requireContext(), 2)
                recyclerView.adapter = adapter
            }
            R.id.action_view_list -> {
                PreferencesHelper.setItemType(requireContext(), "list")
                recyclerView.layoutManager =
                    LinearLayoutManager(requireContext())
                recyclerView.adapter = adapter
            }
        }
        return true

list grill

3. Create a shared preferences activity

Add preferences dependency

implementation 'androidx.preference:preference:1.1.1'

Add Activity settings

    <activity
           android:name=".SettingsActivity"
           android:label="@string/title_activity_settings"></activity>

settings

4. Read default shared preferences

I want to use one of its settings to change the title displayed in the tool bar.
And get the value from the preferences. Then I'll call the function getDefualtSharedPreferences.

implementation 'androidx.preference:preference:1.1.1'

Next I'll set the value of that mutable live data object and I'll use a concatenate string that looks like this, "Stickers for" and then I'll use that variable signature.

   fun updateActivityTitle() {
        val signature =
            PreferenceManager.getDefaultSharedPreferences(app)
                .getString("signature", "Monster fan")
        activityTitle.value = "Stickers for $signature"
    }

anelcc. setText. elizabeth

@AnelCC AnelCC merged commit 115b4e4 into master May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant