询问权限后重新创建活动

我一直在面对这个问题。

我有一个带有开关视图的片段->单击此开关时,我调用一个viewModel方法,该方法要求启用此开关所需的权限。

问题是,每次我请求许可并允许或拒绝时,都会重新创建活动,并且具有开关的片段消失,从而显示活动流的第一个片段(我们可以将其称为功能的第一个片段)

我已经尝试过使用onSaveInstancestate,但是我不清楚在询问权限而不是创建所有内容之后如何检索片段实例。

知道我该如何解决吗?

活动:

class Activity : DaggerActivity<DaggerComponent>() {

private val viewModel: ActivityViewModel by injectionViewModels {
    activityComponent.activityViewModel
}

override fun onSafeCreate(savedInstanceState: Bundle?) {
    super.onSafeCreate(savedInstanceState)
    setContentView(R.layout.single_fragment_activity)

    if (savedInstanceState != null) {
        onRestoreInstanceState(savedInstanceState)
    } else
        observeViewModel()
}

override fun onSafeResume() {
    super.onSafeResume()
    viewModel.onCheckFeatureState()
}

private fun observeViewModel() {
    observe(viewModel.areAllPermissionsGiven) { areGiven ->
        if (areGiven) {
            replaceFragment(SettingsFragment(), R.id.fragment_container, false)
        } else {
            replaceFragment(PermissionsFragment(), R.id.fragment_container, false)
        }
    }
}}

MyFragment:

class MyFragment : DaggerFragment<DaggerComponent>() {

private val viewModel: FragmentViewModel by injectionActivityViewModels {
    activityComponent.fragmentViewModel
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initialize()
}

private fun initialize() {
    viewModel.onViewCreated()
    toolbar.setNavigationOnClickListener { activity?.onBackPressed() }
    initViews()
    initObservables()
}

private fun initViews() {
    switch.onClick {
        viewModel.onToggleMonitoringSecurityClick()
    }
}

private fun initObservables() {
    observe(viewModel.state) { state ->
        switch.isChecked = state.isEnabled
        switchText.isGone = state.hasPermission
    }
}}

我用于请求许可并使用共享首选项启用功能的代码:

class Option @AssistedInject constructor(
@Assisted private val permission: Permission,
@Assisted private val dataSource: DataSource,
private val permissionStatus: GetPermissionStatusUseCase,
private val permissionRequest: RequestPermissionUseCase) {
fun isEnabled() = dataSource.isEnabled && hasPermission

suspend fun enable() = withContext(Dispatchers.IO) {
    val enable = hasPermission || (permissionRequest(permission) is Response.Success)

    if (enable) {
        dataSource.isEnabled = true
    }

    enable
}

fun disable() {
    dataSource.isEnabled = false
}

suspend fun toggle() {
    if (isEnabled()) {
        disable()
    } else {
        enable()
    }
}

val hasPermission
    get() = permissionStatus.invoke(permission) == PermissionStatus.ENABLED

@AssistedInject.Factory
interface Factory {
    fun create(permission: Permission, dataSource: DataSource): Option
}}

我在useCase上使用了上面的域对象代码,该代码提供给viewModel是否具有权限的返回值。

评论