From 24505cd1119c85bd1d68513c637b04be24ca7801 Mon Sep 17 00:00:00 2001 From: canyie Date: Mon, 7 Mar 2022 01:04:21 +0800 Subject: [PATCH] Prevent destroyed activities from being reused The adapter will cache a LayoutInflater which refers the current activity, and the ViewModel object will keep alive until activity finished. After activity recreates (e.g. split-screen), it will use the cached LayoutInflater which refers a destroyed activity and crashes. This also is a memory-leak, according to Google's official document, ViewModel shouldn't refer activity. See https://developer.android.com/topic/libraries/architecture/viewmodel Fix topjohnwu#5413 --- .../main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt | 3 +++ .../java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt | 2 -- .../com/topjohnwu/magisk/ui/superuser/SuperuserFragment.kt | 3 +++ .../com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt | 2 -- app/src/main/res/layout/fragment_module_md2.xml | 1 - app/src/main/res/layout/fragment_superuser_md2.xml | 1 - 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt index 6da0d7b4e..4eadb64a4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleFragment.kt @@ -5,6 +5,8 @@ import android.view.View import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseFragment import com.topjohnwu.magisk.databinding.FragmentModuleMd2Binding +import com.topjohnwu.magisk.databinding.RvItem +import com.topjohnwu.magisk.databinding.adapterOf import com.topjohnwu.magisk.di.viewModel import rikka.recyclerview.addEdgeSpacing import rikka.recyclerview.addInvalidateItemDecorationsObserver @@ -26,6 +28,7 @@ class ModuleFragment : BaseFragment() { super.onViewCreated(view, savedInstanceState) binding.moduleList.apply { + adapter = adapterOf() addEdgeSpacing(top = R.dimen.l_50, bottom = R.dimen.l1) addItemSpacing(R.dimen.l1, R.dimen.l_50, R.dimen.l1) fixEdgeEffect() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt index 34f11e694..7d9a67abc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt @@ -8,7 +8,6 @@ import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.model.module.LocalModule import com.topjohnwu.magisk.core.model.module.OnlineModule import com.topjohnwu.magisk.databinding.RvItem -import com.topjohnwu.magisk.databinding.adapterOf import com.topjohnwu.magisk.databinding.diffListOf import com.topjohnwu.magisk.databinding.itemBindingOf import com.topjohnwu.magisk.events.SelectModuleEvent @@ -26,7 +25,6 @@ class ModuleViewModel : BaseViewModel() { private val itemsInstalled = diffListOf() - val adapter = adapterOf() val items = MergeObservableList() val itemBinding = itemBindingOf { it.bindExtra(BR.viewModel, this) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserFragment.kt index 6fbfbc01e..2e63d4251 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserFragment.kt @@ -4,7 +4,9 @@ import android.os.Bundle import android.view.View import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseFragment +import com.topjohnwu.magisk.databinding.AnyDiffRvItem import com.topjohnwu.magisk.databinding.FragmentSuperuserMd2Binding +import com.topjohnwu.magisk.databinding.adapterOf import com.topjohnwu.magisk.di.viewModel import rikka.recyclerview.addEdgeSpacing import rikka.recyclerview.addItemSpacing @@ -24,6 +26,7 @@ class SuperuserFragment : BaseFragment() { super.onViewCreated(view, savedInstanceState) binding.superuserList.apply { + adapter = adapterOf() addEdgeSpacing(top = R.dimen.l_50, bottom = R.dimen.l1) addItemSpacing(R.dimen.l1, R.dimen.l_50, R.dimen.l1) fixEdgeEffect() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt index ffde23fc9..50dcff0ee 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt @@ -10,7 +10,6 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.utils.BiometricHelper import com.topjohnwu.magisk.core.utils.currentLocale import com.topjohnwu.magisk.databinding.AnyDiffRvItem -import com.topjohnwu.magisk.databinding.adapterOf import com.topjohnwu.magisk.databinding.diffListOf import com.topjohnwu.magisk.databinding.itemBindingOf import com.topjohnwu.magisk.events.SnackbarEvent @@ -33,7 +32,6 @@ class SuperuserViewModel( private val itemsPolicies = diffListOf() private val itemsHelpers = ObservableArrayList() - val adapter = adapterOf() val items = MergeObservableList() .insertList(itemsHelpers) .insertList(itemsPolicies) diff --git a/app/src/main/res/layout/fragment_module_md2.xml b/app/src/main/res/layout/fragment_module_md2.xml index c08c9283f..1d5548baa 100644 --- a/app/src/main/res/layout/fragment_module_md2.xml +++ b/app/src/main/res/layout/fragment_module_md2.xml @@ -19,7 +19,6 @@