Force application to restart from first Activity (when a permission is denied)

后端 未结 5 1819
庸人自扰
庸人自扰 2021-02-02 09:46

It is known that when we deny permissions at runtime in Android 6.0 and resume the app from the recent menu, the app process

5条回答
  •  清歌不尽
    2021-02-02 10:05

    You should put code to check the permission status in your onResume callback. If the user is switching to the System settings permission activity, your Activity will get paused. If the user enables a permission and then returns to your Activity, then onResume will be called, and you will have an opportunity to check for the new permission at that point. Then, you can do whatever you need to restart your activity, such as calling startActivity with an Intent that has FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK and launches your Activity again.

    override fun onResume() {
      super.onResume()
    
      // check for permission
      checkPermissionsAndRestartIfNecessary()
    }
    
    private fun checkPermissionsAndRestartIfNecessary() {
      if (ContextCompat.checkSelfPermission(...) {
        ...
      }
    }
    

提交回复
热议问题