Make sure to create PreferenceScreen only once

This commit is contained in:
Rocka 2026-03-12 02:00:49 +08:00
parent c88ed1f31e
commit fe3a618c8f
No known key found for this signature in database
GPG Key ID: 28031158FFDD6853

View File

@ -5,6 +5,7 @@
package org.fcitx.fcitx5.android.ui.main.settings package org.fcitx.fcitx5.android.ui.main.settings
import android.os.Bundle import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@ -77,18 +78,19 @@ abstract class FcitxPreferenceFragment : PaddingPreferenceFragment() {
* If that fragment was derived from `FcitxPreferenceFragment`, it needs to call `obtainConfig` * If that fragment was derived from `FcitxPreferenceFragment`, it needs to call `obtainConfig`
* which would need the route params, and in turn needs `NavGraph`. * which would need the route params, and in turn needs `NavGraph`.
* But at this time it's still in `MainActivity`'s `super.onCreate`, the Activity did not have * But at this time it's still in `MainActivity`'s `super.onCreate`, the Activity did not have
* chance to setup `NavGraph` on `navController`, so accessing `lazyRoute` would crash. * chance to set up `NavGraph` on `navController`, so accessing `lazyRoute` would crash.
* *
* That is to say, if we declare `app:navGraph` on `<FragmentContainerView />` in `activity_main.xml`, * That is to say, if we declare `app:navGraph` on `<FragmentContainerView />` in `activity_main.xml`,
* the graph would have been initialized when `NavHostFragment` got inflated, and does not suffer * the graph would have been initialized when `NavHostFragment` got inflated, and does not suffer
* from this problem? But maintain navigation destinations in xml is too tedious ... * from this problem? But maintain navigation destinations in XML is too tedious ...
*/ */
final override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { final override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
} }
override fun onStart() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onStart() super.onViewCreated(view, savedInstanceState)
viewModel.setToolbarTitle(getPageTitle()) // make sure to create preference only once since `onViewCreated` is also called on Fragment resume
if (preferenceScreen?.isEmpty() == false) return
val context = requireContext() val context = requireContext()
lifecycleScope.withLoadingDialog(context) { lifecycleScope.withLoadingDialog(context) {
raw = fcitx.runOnReady { obtainConfig(this) } raw = fcitx.runOnReady { obtainConfig(this) }
@ -109,4 +111,9 @@ abstract class FcitxPreferenceFragment : PaddingPreferenceFragment() {
viewModel.disableAboutButton() viewModel.disableAboutButton()
} }
} }
override fun onStart() {
super.onStart()
viewModel.setToolbarTitle(getPageTitle())
}
} }