1
mirror of https://github.com/topjohnwu/Magisk synced 2024-11-16 00:13:57 +01:00

Fix crash when fragment is detached from activity

This commit is contained in:
LoveSy 2022-01-27 17:54:24 +08:00 committed by GitHub
parent f5e5ab2436
commit 1893359142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 18 additions and 18 deletions

View File

@ -15,11 +15,11 @@ import com.topjohnwu.magisk.ktx.startAnimations
abstract class BaseFragment<Binding : ViewDataBinding> : Fragment(), ViewModelHolder {
val activity get() = requireActivity() as NavigationActivity<*>
val activity get() = getActivity() as? NavigationActivity<*>
protected lateinit var binding: Binding
protected abstract val layoutRes: Int
private val navigation get() = activity.navigation
private val navigation get() = activity?.navigation
open val snackbarAnchorView: View? get() = null
override fun onCreate(savedInstanceState: Bundle?) {
@ -41,12 +41,12 @@ abstract class BaseFragment<Binding : ViewDataBinding> : Fragment(), ViewModelHo
override fun onStart() {
super.onStart()
activity.supportActionBar?.subtitle = null
activity?.supportActionBar?.subtitle = null
}
override fun onEventDispatched(event: ViewEvent) = when(event) {
is ContextExecutor -> event(requireContext())
is ActivityExecutor -> event(activity)
is ActivityExecutor -> activity?.let { event(it) } ?: Unit
is FragmentExecutor -> event(this)
else -> Unit
}

View File

@ -83,7 +83,7 @@ class SelectModuleEvent : ViewEvent(), FragmentExecutor {
override fun invoke(fragment: BaseFragment<*>) {
try {
fragment.apply {
activity.getContent("application/zip") {
activity?.getContent("application/zip") {
MainDirections.actionFlashFragment(Const.Value.FLASH_ZIP, it).navigate()
}
}

View File

@ -26,7 +26,7 @@ class DenyListFragment : BaseFragment<FragmentDenyMd2Binding>() {
override fun onAttach(context: Context) {
super.onAttach(context)
activity.setTitle(R.string.denylist)
activity?.setTitle(R.string.denylist)
setHasOptionsMenu(true)
}

View File

@ -31,10 +31,10 @@ class FlashFragment : BaseFragment<FragmentFlashMd2Binding>() {
override fun onStart() {
super.onStart()
setHasOptionsMenu(true)
activity.setTitle(R.string.flash_screen_title)
activity?.setTitle(R.string.flash_screen_title)
viewModel.subtitle.observe(this) {
activity.supportActionBar?.setSubtitle(it)
activity?.supportActionBar?.setSubtitle(it)
}
}
@ -49,15 +49,15 @@ class FlashFragment : BaseFragment<FragmentFlashMd2Binding>() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
defaultOrientation = activity.requestedOrientation
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
defaultOrientation = activity?.requestedOrientation ?: -1
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
viewModel.startFlashing()
}
@SuppressLint("WrongConstant")
override fun onDestroyView() {
if (defaultOrientation != -1) {
activity.requestedOrientation = defaultOrientation
activity?.requestedOrientation = defaultOrientation
}
super.onDestroyView()
}

View File

@ -19,7 +19,7 @@ class HomeFragment : BaseFragment<FragmentHomeMd2Binding>() {
override fun onStart() {
super.onStart()
activity.title = resources.getString(R.string.section_home)
activity?.title = resources.getString(R.string.section_home)
setHasOptionsMenu(true)
DownloadService.observeProgress(this, viewModel::onProgressUpdate)
}
@ -64,7 +64,7 @@ class HomeFragment : BaseFragment<FragmentHomeMd2Binding>() {
when (item.itemId) {
R.id.action_settings ->
HomeFragmentDirections.actionHomeFragmentToSettingsFragment().navigate()
R.id.action_reboot -> RebootEvent.inflateMenu(activity).show()
R.id.action_reboot -> activity?.let { RebootEvent.inflateMenu(it).show() }
else -> return super.onOptionsItemSelected(item)
}
return true

View File

@ -38,7 +38,7 @@ class LogFragment : BaseFragment<FragmentLogMd2Binding>() {
override fun onStart() {
super.onStart()
setHasOptionsMenu(true)
activity.title = resources.getString(R.string.logs)
activity?.title = resources.getString(R.string.logs)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View File

@ -19,7 +19,7 @@ class ModuleFragment : BaseFragment<FragmentModuleMd2Binding>() {
override fun onStart() {
super.onStart()
setHasOptionsMenu(true)
activity.title = resources.getString(R.string.modules)
activity?.title = resources.getString(R.string.modules)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View File

@ -18,7 +18,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsMd2Binding>() {
override fun onStart() {
super.onStart()
activity.title = resources.getString(R.string.settings)
activity?.title = resources.getString(R.string.settings)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View File

@ -17,7 +17,7 @@ class SuperuserFragment : BaseFragment<FragmentSuperuserMd2Binding>() {
override fun onStart() {
super.onStart()
activity.title = resources.getString(R.string.superuser)
activity?.title = resources.getString(R.string.superuser)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View File

@ -61,7 +61,7 @@ class ThemeFragment : BaseFragment<FragmentThemeMd2Binding>() {
override fun onStart() {
super.onStart()
activity.title = getString(R.string.section_theme)
activity?.title = getString(R.string.section_theme)
}
}