Manage MenuProvider with lifecycle state

This commit is contained in:
topjohnwu 2023-06-22 16:12:35 -07:00
parent 6c09fc2e64
commit fbd1dbb20c

View File

@ -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<Binding : ViewDataBinding> : 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<Binding : ViewDataBinding> : 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<Binding : ViewDataBinding> : 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()
}
}