From 33a943fbec0a8b6f176eb4cf174a56a199276055 Mon Sep 17 00:00:00 2001 From: Conn MyLong <192296260+100kg-5km@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:59:55 +0800 Subject: [PATCH] Fix FragmentStateAdapter recreates Fragments --- .../java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt | 1 + .../main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt b/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt index 47daa2f..b851bb5 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt @@ -69,6 +69,7 @@ class GroupsFragment : Fragment(), CommandClient.Handler { override fun onDestroyView() { super.onDestroyView() binding = null + commandClient.disconnect() } private var displayed = false diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt b/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt index 7258678..1068f2d 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt @@ -40,10 +40,11 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { return binding.root } + private val adapter by lazy { Adapter(this) } private fun onCreate() { val activity = activity ?: return val binding = binding ?: return - binding.dashboardPager.adapter = Adapter(this) + binding.dashboardPager.adapter = adapter binding.dashboardPager.offscreenPageLimit = Page.values().size activity.serviceStatus.observe(viewLifecycleOwner) { when (it) { @@ -107,6 +108,7 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { super.onDestroyView() mediator?.detach() mediator = null + binding?.dashboardPager?.adapter = null binding = null }