onSharedPreferenceChanged doesn’t proc, Kotlin/Android

  android, kotlin

So I have my settings activity set up with my < PreferenceScreen > for choosing the app settings.
What I want to do is have my changes do immediate effect, and not on app restart.
Naturally I wanted to use OnSharedPreferenceChangeListener , but if I try to put a Log in onSharedPreferenceChanged, it never procs on changed selection. My plan was to recreate() on preference changed, so my preference apply function would proc on onCreate() with that recreate().
The problem is, as previously stated that onSharedPreferenceChanged never procs.

I tried registering the listener as it was stated somewhere, but it didn’t help.

Can anyone help me?

class SettingsActivity : AppCompatActivity(),
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
SharedPreferences.OnSharedPreferenceChangeListener {


val Tag = "My Activity:"

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val prefMen = SharedPreferencesManager(this)
    prefMen.loadTheme()

    setContentView(R.layout.activity_settings)

    if (savedInstanceState == null) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.content_preference, MainPreference()).commit()
    } else {
        title = savedInstanceState.getCharSequence(TAG_TITLE)
    }

    supportFragmentManager.addOnBackStackChangedListener {
        if (supportFragmentManager.backStackEntryCount == 0) {
            setTitle("Settings")
        }
    }

    setUpToolbar()

}

override fun onStart() {
    super.onStart()
    getPreferences(MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this)
}

override fun onDestroy() {
    super.onDestroy()
    getPreferences(MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this)

}

override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
    super.onSaveInstanceState(outState, outPersistentState)
    outState.putCharSequence(TAG_TITLE, title)
}

override fun onSupportNavigateUp(): Boolean {
    if (supportFragmentManager.popBackStackImmediate()) {
        return true
    }
    return super.onSupportNavigateUp()
}

private fun setUpToolbar() {
    supportActionBar?.setTitle("Settings")
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    supportActionBar?.setDisplayShowHomeEnabled(true)

}

class MainPreference : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)
    }

}

override fun onPreferenceStartFragment(
    caller: PreferenceFragmentCompat?,
    pref: Preference?
): Boolean {
    val args = pref?.extras
    val fragment = pref?.fragment?.let {
        supportFragmentManager.fragmentFactory.instantiate(
            classLoader,
            it
        ).apply {
            arguments = args
            setTargetFragment(caller, 0)
        }
    }
    fragment?.let {
        supportFragmentManager.beginTransaction().replace(R.id.content_preference, it)
            .addToBackStack(null).commit()
    }

    title = pref?.title

    return true
}

companion object {
    private val TAG_TITLE = "PREFERENCE_ACTIVITY"
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
    recreate()
}

}

Source: Android Questions

LEAVE A COMMENT