trying to fix a bug where the shareed prferences listener has no context - not yet succeeded :-)

This commit is contained in:
y20k 2020-01-31 14:51:31 +01:00
parent e97a1e6661
commit 6a989a8fc3
No known key found for this signature in database
GPG key ID: 824D4259F41FAFF6
4 changed files with 22 additions and 4 deletions

View file

@ -89,6 +89,7 @@ class MainActivity : AppCompatActivity() {
} }
/* Overrides onDestroy from AppCompatActivity */
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
// unregister listener for changes in shared preferences // unregister listener for changes in shared preferences

View file

@ -60,6 +60,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
private lateinit var currentBestLocation: Location private lateinit var currentBestLocation: Location
private lateinit var layout: MapFragmentLayoutHolder private lateinit var layout: MapFragmentLayoutHolder
private lateinit var trackerService: TrackerService private lateinit var trackerService: TrackerService
private lateinit var sharedPreferenceChangeListener2:SharedPreferences.OnSharedPreferenceChangeListener
/* Overrides onCreate from Fragment */ /* Overrides onCreate from Fragment */
@ -249,6 +250,21 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
*/ */
private fun createSharedPreferenceChangeListener(): SharedPreferences.OnSharedPreferenceChangeListener {
return SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
when (key) {
Keys.PREF_TRACKING_STATE -> {
if (isAdded) {
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
layout.updateRecordingButton(trackingState)
}
}
}
}
}
/* /*
* Defines callbacks for service binding, passed to bindService() * Defines callbacks for service binding, passed to bindService()
*/ */
@ -259,7 +275,8 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
trackerService = binder.service trackerService = binder.service
bound = true bound = true
// register listener for changes in shared preferences // register listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) sharedPreferenceChangeListener2 = createSharedPreferenceChangeListener()
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener2)
// start listening for location updates // start listening for location updates
handler.removeCallbacks(periodicLocationRequestRunnable) handler.removeCallbacks(periodicLocationRequestRunnable)
handler.postDelayed(periodicLocationRequestRunnable, 0) handler.postDelayed(periodicLocationRequestRunnable, 0)
@ -267,7 +284,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
override fun onServiceDisconnected(arg0: ComponentName) { override fun onServiceDisconnected(arg0: ComponentName) {
bound = false bound = false
// unregister listener for changes in shared preferences // unregister listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener2)
// stop receiving location updates // stop receiving location updates
handler.removeCallbacks(periodicLocationRequestRunnable) handler.removeCallbacks(periodicLocationRequestRunnable)
} }

View file

@ -38,7 +38,7 @@ class Trackbook: Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
LogHelper.v(TAG, "Trackbook application started.") LogHelper.v(TAG, "Trackbook application started.")
// set Day / Night theme state // set Dark / Light theme state
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection(this)) AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection(this))
} }

View file

@ -1,7 +1,7 @@
/* /*
* AppThemeHelper.kt * AppThemeHelper.kt
* Implements the AppThemeHelper object * Implements the AppThemeHelper object
* A AppThemeHelper can set the differnt app themes: Light Mode, Dark Mode, Follow System * A AppThemeHelper can set the different app themes: Light Mode, Dark Mode, Follow System
* *
* This file is part of * This file is part of
* TRACKBOOK - Movement Recorder for Android * TRACKBOOK - Movement Recorder for Android