From 12690c699c83554e48bd9c901ff80bc4add84225 Mon Sep 17 00:00:00 2001 From: Ishan09811 <156402647+Ishan09811@users.noreply.github.com> Date: Sat, 14 Dec 2024 21:56:34 +0530 Subject: [PATCH] recreate the activity when turning on or off material colors instead of restarting the entire application --- .../main/java/org/stratoemu/strato/StratoApplication.kt | 6 ++++++ .../stratoemu/strato/settings/GlobalSettingsFragment.kt | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/stratoemu/strato/StratoApplication.kt b/app/src/main/java/org/stratoemu/strato/StratoApplication.kt index c993a57d7..8c9dce98b 100644 --- a/app/src/main/java/org/stratoemu/strato/StratoApplication.kt +++ b/app/src/main/java/org/stratoemu/strato/StratoApplication.kt @@ -29,6 +29,12 @@ class StratoApplication : Application() { private set val context : Context get() = instance.applicationContext + + fun setTheme(newValue: Boolean) { + val dynamicColorsOptions = DynamicColorsOptions.Builder().setPrecondition { _, _ -> newValue }.build() + DynamicColors.applyToActivitiesIfAvailable(instance, dynamicColorsOptions) + if (newValue == false) { instance.setTheme(R.style.AppTheme) } + } } override fun onCreate() { diff --git a/app/src/main/java/org/stratoemu/strato/settings/GlobalSettingsFragment.kt b/app/src/main/java/org/stratoemu/strato/settings/GlobalSettingsFragment.kt index a2454d1cf..5a9f93bc6 100644 --- a/app/src/main/java/org/stratoemu/strato/settings/GlobalSettingsFragment.kt +++ b/app/src/main/java/org/stratoemu/strato/settings/GlobalSettingsFragment.kt @@ -20,6 +20,7 @@ import org.stratoemu.strato.MainActivity import org.stratoemu.strato.R import org.stratoemu.strato.utils.GpuDriverHelper import org.stratoemu.strato.utils.WindowInsetsHelper +import org.stratoemu.strato.StratoApplication import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -45,9 +46,10 @@ class GlobalSettingsFragment : PreferenceFragmentCompat() { addPreferencesFromResource(R.xml.credits_preferences) // Re-launch the app if Material You is toggled - findPreference("use_material_you")?.setOnPreferenceChangeListener { _, _ -> - requireActivity().finishAffinity() - startActivity(Intent(requireContext(), MainActivity::class.java)) + findPreference("use_material_you")?.setOnPreferenceChangeListener { _, newValue -> + val isMaterialYouEnabled = newValue as Boolean + StratoApplication.setTheme(isMaterialYouEnabled) + requireActivity().recreate() true }