From fbd1dbb20c0fa11f8db325f6d88352046c2b2531 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 22 Jun 2023 16:12:35 -0700 Subject: [PATCH] Manage MenuProvider with lifecycle state --- .../java/com/topjohnwu/magisk/arch/BaseFragment.kt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt b/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt index b996eec39..526c7c00b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt @@ -10,6 +10,7 @@ import androidx.databinding.DataBindingUtil import androidx.databinding.OnRebindCallback import androidx.databinding.ViewDataBinding import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle import androidx.navigation.NavDirections import com.topjohnwu.magisk.BR @@ -37,6 +38,9 @@ abstract class BaseFragment : Fragment(), ViewModelHo it.setVariable(BR.viewModel, viewModel) it.lifecycleOwner = viewLifecycleOwner } + if (this is MenuProvider) { + activity?.addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.STARTED) + } savedInstanceState?.let { viewModel.onRestoreState(it) } return binding.root } @@ -48,9 +52,6 @@ abstract class BaseFragment : Fragment(), ViewModelHo override fun onStart() { super.onStart() activity?.supportActionBar?.subtitle = null - if (this is MenuProvider) { - activity?.addMenuProvider(this, viewLifecycleOwner) - } } override fun onEventDispatched(event: ViewEvent) = when(event) { @@ -92,11 +93,4 @@ abstract class BaseFragment : Fragment(), ViewModelHo fun NavDirections.navigate() { navigation?.currentDestination?.getAction(actionId)?.let { navigation!!.navigate(this) } } - - override fun onStop() { - if (this is MenuProvider) { - activity?.removeMenuProvider(this) - } - super.onStop() - } }