The Android app does not reflect the settings.

Asked 2 years ago, Updated 2 years ago, 120 views

I created the settings screen using the PreferenceLibrary and kotlin, but once I close the application and reopen it, it will be reflected, but if I use it as it is, the settings will not be reflected.How can I reflect it each time?

"The app I am making is a calculator, so every time I press ""="", I made them do the following, but it didn't work."

valsharedPreferences=PreferenceManager.getDefaultSharedPreferences(this)
             var mode = sharedPreferences.getString("mode", "include")
            var LEVEL = sharedPreferences.getString("level", "OFF")

Source Code

setting.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="OnOff"
        android:summaryOn="On"
        android:summaryOff="Off"
        android: title="OnOff"/>

    <ListPreference
        android:defaultValue="OFF"
        android:entries="@array/reply_entries"
        android:entryValues="@array/reply_values"
        android:key="level"
        android: title="List preference"
        app:useSimpleSummaryProvider="true"/>

</PreferenceScreen>

Seeting.kt

package com.example.myapp

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.preference.PreferenceFragmentCompat
import com.example.sugoidentaku.R

class Setting:PreferenceFragmentCompat(){
    override fun onCreatePreferences (savedInstanceState: Bundle?, rootKey: String?) {
        // Configure the settings screen according to the res/xml/preferences.xml file
        setPreferencesFromResource(R.xml.setting, rootKey)
    }

MainActivity.kt

valsharedPreferences=PreferenceManager.getDefaultSharedPreferences(this)
        var mode = sharedPreferences.getString("mode", "include")
        var LEVEL = sharedPreferences.getString("level", "OFF")

 supportFragmentManager
            .beginTransaction()
            .replace(R.id.setting,Setting())
            .commit() 

android kotlin

2022-09-30 15:57

1 Answers

As far as I can see, it looks like I'm setting the settings from the Android Studio template.

For android:key="OnOff" in CheckBoxPreference,

var mode=sharedPreferences.getString("mode", "increase")

So I'm going to do this

 varonoff=sharedPreferences.getBoolean("OnOff", false)

Make sure that you have .
Also,

android:entries="@array/reply_entries"
    android:entryValues="@array/reply_values"

This seems to remain a template, so please check res/values/arrays.xml.

<string-array name="reply_values">
    <item>reply</item>
    <item>reply_all</item>
</string-array>

If this is the case, the return value for the level will be reply or reply_all.


2022-09-30 15:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.