Skip to content

Commit

Permalink
fix(workaround): restart application on settings change
Browse files Browse the repository at this point in the history
  • Loading branch information
butzist committed Feb 17, 2024
1 parent 65edee6 commit aefefbf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package de.szalkowski.activitylauncher
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import de.szalkowski.activitylauncher.services.SettingsService
import de.szalkowski.activitylauncher.ui.SettingsFragment
import javax.inject.Inject


@AndroidEntryPoint
class SettingsActivity : AppCompatActivity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package de.szalkowski.activitylauncher.ui

import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Resources
import android.os.Bundle
import android.widget.Toast
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreference
import dagger.hilt.android.AndroidEntryPoint
import de.szalkowski.activitylauncher.MainActivity
import de.szalkowski.activitylauncher.R
import de.szalkowski.activitylauncher.services.RootDetectionService
import de.szalkowski.activitylauncher.services.SettingsService
Expand All @@ -19,13 +20,34 @@ import javax.inject.Inject
@AndroidEntryPoint
class SettingsFragment : PreferenceFragmentCompat() {
private lateinit var prefs: SharedPreferences
private var needsRestart: Boolean = false

@Inject
internal lateinit var rootDetectionService: RootDetectionService

@Inject
internal lateinit var settingsService: SettingsService

override fun onDestroy() {
super.onDestroy()
if (!needsRestart) return

// workaround for applying settings by restarting app - PRs welcome
// FIXME reset the services state and reload affected activities
restartApp()
}

private fun restartApp() {
val intent = Intent(
this.requireContext(),
MainActivity::class.java,
)

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
this.startActivity(intent)
this.requireActivity().finishAffinity()
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().baseContext)
Expand Down Expand Up @@ -71,6 +93,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
Toast.makeText(activity, getText(R.string.warning_root_check), Toast.LENGTH_LONG).show()
}
prefs.edit().putBoolean("allow_root", newValue).apply()
needsRestart = true
return true
}

Expand All @@ -82,14 +105,15 @@ class SettingsFragment : PreferenceFragmentCompat() {

private fun onHidePrivateUpdated(newValue: Boolean): Boolean {
prefs.edit().putBoolean("hide_hide_private", newValue).apply()
needsRestart = true
return true
}

private fun onLanguageUpdated(newValue: String): Boolean {
prefs.edit().putString("language", newValue).apply()

settingsService.applyLocaleConfiguration(requireActivity().baseContext)
requireActivity().recreate()
needsRestart = true
return true
}
}
Expand Down

0 comments on commit aefefbf

Please sign in to comment.