From: Soren Stoutner Date: Tue, 29 Nov 2022 22:22:59 +0000 (-0700) Subject: Restore the scroll position when restarting the Settings activity. https://redmine... X-Git-Tag: v1.9~4 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=81346128a1dcde5b65d83f24c1af507cd80802d3;p=PrivacyCell.git Restore the scroll position when restarting the Settings activity. https://redmine.stoutner.com/issues/913 --- diff --git a/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt index 9adae22..dc8545f 100644 --- a/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt @@ -40,6 +40,9 @@ import com.stoutner.privacycell.activities.PrivacyCellActivity import com.stoutner.privacycell.dialogs.NotificationPermissionDialog import com.stoutner.privacycell.services.RealtimeMonitoringService +// Define the class constants. +private const val SCROLL_Y = "scroll_y" + class SettingsFragment : PreferenceFragmentCompat() { // Declare the class variables. private lateinit var sharedPreferenceChangeListener: OnSharedPreferenceChangeListener @@ -52,6 +55,12 @@ class SettingsFragment : PreferenceFragmentCompat() { private lateinit var consider3gAntiquatedPreference: Preference private lateinit var bottomAppBarPreference: Preference + companion object { + // Declare the private static class variables. Otherwise, onRestart will not pull the same values that are populated from the saved instance state. + private var fragmentRestarted: Boolean = false + private var scrollY: Int = 0 + } + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { // Load the preferences from the XML file. setPreferencesFromResource(R.xml.preferences, rootKey) @@ -128,36 +137,15 @@ class SettingsFragment : PreferenceFragmentCompat() { } else { bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled) } - } - - // The listener should be unregistered when the app is paused. - override fun onPause() { - // Run the default commands. - super.onPause() - // Get a handle for the shared preferences. - val sharedPreferences = preferenceScreen.sharedPreferences!! + // Check if the fragment has been restarted. + if (savedInstanceState != null) { + // Set the fragment restored flag. + fragmentRestarted = true - // Unregister the shared preference listener. - sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) - } - - // The listener should be re-registered when the app is resumed. - override fun onResume() { - // Run the default commands. - super.onResume() - - // Get a new shared preference change listener. - sharedPreferenceChangeListener = getSharedPreferenceChangeListener() - - // Get a handle for the shared preferences. - val sharedPreferences = preferenceScreen.sharedPreferences!! - - // Re-register the shared preference listener. - sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) - - // Update the realtime monitoring preference summary. - updateRealtimeMonitoringSummary() + // Save the scroll Y. + scrollY = savedInstanceState.getInt(SCROLL_Y) + } } private fun getSharedPreferenceChangeListener(): OnSharedPreferenceChangeListener { @@ -244,6 +232,53 @@ class SettingsFragment : PreferenceFragmentCompat() { } } + // The listener should be unregistered when the app is paused. + override fun onPause() { + // Run the default commands. + super.onPause() + + // Get a handle for the shared preferences. + val sharedPreferences = preferenceScreen.sharedPreferences!! + + // Unregister the shared preference listener. + sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) + } + + // The listener should be re-registered when the app is resumed. + override fun onResume() { + // Run the default commands. + super.onResume() + + // Get a new shared preference change listener. + sharedPreferenceChangeListener = getSharedPreferenceChangeListener() + + // Get a handle for the shared preferences. + val sharedPreferences = preferenceScreen.sharedPreferences!! + + // Re-register the shared preference listener. + sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) + + // Update the realtime monitoring preference summary. + updateRealtimeMonitoringSummary() + + // Restore the scroll position if the fragment has been restarted. + if (fragmentRestarted) { + // Reset the fragment restarted flag. + fragmentRestarted = false + + // Set the scroll position. + listView.smoothScrollBy(0, scrollY) + } + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + // Run the default commands. + super.onSaveInstanceState(savedInstanceState) + + // Save the scroll position. + savedInstanceState.putInt(SCROLL_Y, listView.computeVerticalScrollOffset()) + } + private fun restartPrivacyCell() { // Create an intent to restart Privacy Cell. val restartIntent = requireActivity().parentActivityIntent!! diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 7ff040c..a839c1c 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -132,6 +132,7 @@ Monitorización Monitorización en tiempo real Coloca un icono en la barra de estado que monitoriza la red celular. + El permiso de publicación de notificaciones está actualmente denegado, por lo que no se mostrarán las notificaciones. Notificación de red segura Notificación de red insegura Notificación de red obsoleta diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2b47728..609cde0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -134,7 +134,7 @@ Monitoring Realtime monitoring Place an icon in the status bar that monitors the cell network. - The Post Notification permission is currently denied, so no notifications will be displayed. + The Post Notification permission is currently denied, so notifications will not be displayed. Secure network notification Insecure network notification Antiquated network notification