For my app I am using SharedPreferences to create a setting that allows the user to switch between light and dark theme when using it. My problem is every time I load up the app and turn on the setting nothing happens and how to make my app switch between light and dark theme using a SwitchPreference using multiple activities.
For context I am building an app that is connected to Firestore that allows users to add TV show data from the Recycler View list.
I have tried looking for documentation on Google and watching videos online to see if I can find any similarity to my issue, but every time I do there is like 50 different ways of doing so.
This is my activity for the RecyclerView List and some of the SharedPreferences:
override fun onResume(){
super.onResume()
val sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
val isDarkTheme = sharedPreferences.getBoolean("night",false)
if (!isDarkTheme) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
editor.putBoolean("night",false)
editor.apply()
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
editor.putBoolean("night",true)
editor.apply()
}
recyclerViewTV = findViewById(R.id.recyclerViewShows)
recyclerViewTV.layoutManager = LinearLayoutManager(this)
data = ArrayList<TV>()
tvAdapter = TVAdapter(data)
recyclerViewTV.adapter = tvAdapter
db = FirebaseFirestore.getInstance()
db.collection("ProjectTVShows")
.get()
.addOnSuccessListener { result ->
for (document in result) {
var show = document.getString("name")
var direct = document.getString("director")
var view = document.getString("viewership")
data.add(TV(show, direct, view))
Log.d("MY DEBUG", "${document.id} => ${document.data}")
Log.d("MY DEBUG", "show => $show")
Log.d("MY DEBUG", "direct => $direct")
Log.d("MY DEBUG", "view => $view")
}
tvAdapter.notifyDataSetChanged()
}
.addOnFailureListener { exception ->
Log.w("MY DEBUG", "Error getting documents.", exception)
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.toolmenu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.getItemId()) {
R.id.add_shows-> {
val intent = Intent(this@TVShowListActivity, AddShowActivity::class.java)
startActivity(intent)
true
}
R.id.settings->{
val intent = Intent(this@TVShowListActivity, SettingsActivity::class.java)
startActivity(intent)
true
}
else -> super.onOptionsItemSelected(item)
}
}}
This is my UserPreferenceFragment:
class UserPreferenceFragment: PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
This is my preference layout for the light/dark mode setting

This is my preference layout in 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">
<SwitchPreferenceCompat
app:defaultValue="false"
app:key="night"
app:summary="Toggle between light and dark mode"
app:title="@string/light_settings" />
</PreferenceScreen>